problem
747. Largest Number At Least Twice of Others
题意:
solution1:
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = -, secondMx = -, mxId = -;//err...
if(nums.size()<) return -;
for(int i=; i<nums.size(); i++)
{
if(nums[i]>mx)
{
secondMx = mx;
mx = nums[i];
mxId = i;
}
else if(nums[i]>secondMx) secondMx = nums[i];
}
return (*secondMx<=mx) ? mxId : -;
}
};
solution2:
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, mxId = -;//err...
if(nums.size()<) return -;
for(int i=; i<nums.size(); i++)
{
if(nums[i]>mx) { mx = nums[i]; mxId = i; }
}
for(auto num:nums)
{
if(mx!=num && mx-num<num) return -;
}
return mxId;
}
};
参考
1. Leetcode_easy_747. Largest Number At Least Twice of Others;
2. Grandyang;
3. Discuss;
完