【java】747. Largest Number At Least Twice of Others

问题原文点击打开链接

这道题简单的不用思考.....

    public int dominantIndex(int[] nums) {
        int maxVal = Integer.MIN_VALUE;
        int pos = 0;
        for (int i = 0;i<nums.length;i++){
            if (nums[i] > maxVal){
                pos = i;
                maxVal = nums[i];
            }
        }

        for (int i = 0;i<nums.length;i++){
            if (maxVal < 2*nums[i] && i != pos) return -1;
        }
        return pos;
    }

猜你喜欢

转载自blog.csdn.net/amber804105/article/details/80952993
今日推荐