747. Largest Number At Least Twice of Others

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;
    }
};
复制代码

 

posted @   JTechRoad  阅读(112)  评论(0编辑  收藏  举报
编辑推荐:
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
阅读排行:
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· 为什么AI教师难以实现
· 如何让低于1B参数的小型语言模型实现 100% 的准确率
· AI Agent爆火后,MCP协议为什么如此重要!
点击右上角即可分享
微信分享提示