码迷,mamicode.com
首页 > 其他好文 > 详细

747. Largest Number At Least Twice of Others

时间:2018-05-01 14:11:46      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:esc   turn   update   cto   leetcode   style   size   ret   dex   

https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/

class Solution {
public:
    int dominantIndex(vector<int>& nums) {
        if (nums.size() == 0)    return -1;
        int max1 = nums[0], max2, max1index = 0, max2index = -1;
        for (int i = 1; i < nums.size(); i++) {
            if (nums[i] >= max1) {      // we don‘t need to update max1 when it == nums[i] only for this problem
                if (nums[i] > max1) {
                    max2 = max1;
                    max2index = max1index;
                    max1 = nums[i];
                    max1index = i;
                }
            }
            else if (max2index < 0 || nums[i] > max2) {
                max2 = nums[i];
                max2index = i;
            }
        }
        return (max2index < 0 || max1 >= 2 * max2) ? max1index : -1;
    }
};

 

747. Largest Number At Least Twice of Others

标签:esc   turn   update   cto   leetcode   style   size   ret   dex   

原文地址:https://www.cnblogs.com/JTechRoad/p/8976010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!