0747.cpp (457B)
1 class Solution { 2 public: 3 int dominantIndex(vector<int> &nums) { 4 int max1, max2, index; 5 max1 = max2 = index = 0; 6 7 for (int i = 0; i < nums.size(); i++) { 8 if (nums[i] > max1) { 9 index = i; 10 max2 = max1; 11 max1 = nums[i]; 12 } else if (nums[i] > max2) { 13 max2 = nums[i]; 14 } 15 } 16 return 2 * max2 <= max1 ? index : -1; 17 } 18 };