标签:替换 pre mic cto style mamicode 遍历 str alt
题目:

解答:
one、two 表示最大值和第二大值的数字;oneIndex 表示最大值的索引;for 遍历 nums;nums 过程中:如果这个数 nums[i] 比最大值还大,那么替换掉 two、one、oneIndex;如果这个数 nums[i] 比第二大值还大,那么替换掉第二大值 two。one 是否大于或者等于 two * 2,返回 oneIndex 或者 -1。1 class Solution { 2 public: 3 int dominantIndex(vector<int>& nums) 4 { 5 int one = 0; 6 int oneIndex = 0; 7 int two = 0; 8 9 for (int i = 0; i < nums.size(); i++) 10 { 11 if (nums[i] > one) 12 { 13 two = one; 14 one = nums[i]; 15 oneIndex = i; 16 } else if (nums[i] > two) 17 { 18 two = nums[i]; 19 } 20 } 21 22 return one >= two * 2 ? oneIndex : -1; 23 } 24 };
标签:替换 pre mic cto style mamicode 遍历 str alt
原文地址:https://www.cnblogs.com/ocpc/p/12827633.html