1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 int dominantIndex(vector<int>& nums) 12 { 13 int firstmax=INT_MIN,secondmax=INT_MIN,index=-1; 14 int sz=nums.size(); 15 for(int i=0;i<sz;i++) 16 { 17 if(nums[i]>firstmax) 18 { 19 secondmax=firstmax; 20 firstmax=nums[i]; 21 index=i; 22 } 23 else if(nums[i]>secondmax) 24 secondmax=nums[i]; 25 } 26 return (firstmax>=2*secondmax)? index:-1; 27 } 28 };
扫描一遍,找出最大和第二大的就行了
浙公网安备 33010602011771号