• Leetcode刷题——day2


    414. 第三大的数

    class Solution {
        public int thirdMax(int[] nums) {
            int n = nums.length;
            long first, second, third;
            first = second = third = Long.MIN_VALUE;
    
            if(n == 1) return nums[0];
            if(n == 2) return Math.max(nums[0], nums[1]);
    
            for(int i = 0; i < n; i++) {
                if(nums[i] == first || nums[i] == second) continue;
                if(nums[i] > first) {
                    third = second;
                    second = first;
                    first = nums[i];
                    continue;
                }
                if(nums[i] > second) {
                    third = second;
                    second = nums[i];
                    continue;
                }
                if(nums[i] > third) {
                    third = nums[i];
                }
            }
            return third == Long.MIN_VALUE ? (int)first : (int)third;
        }
    }
    

    628. 三个数的最大乘积

    class Solution {
        public int maximumProduct(int[] nums) {
            int n = nums.length;
            int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
            int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
            for(int x : nums) {
                if(x < min1) {
                    min2 = min1;
                    min1 = x;
                } else if(x < min2) {
                    min2 = x;
                }
    
                if(x > max1) {
                    max3 = max2;
                    max2 = max1;
                    max1 = x;
                } else if(x > max2) {
                    max3 = max2;
                    max2 = x;
                } else if(x > max3) {
                    max3 = x;
                }
            }
            return Math.max((max1 * max2 * max3), (min1 * min2 * max1));
        }
    }
    

    作者:lykxbg —— 来一块小饼干

    出处:http://www.cnblogs.com/lykxbg/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Web后门工具WeBaCoo
    苹果内存取证工具volafox
    PlayMaker GUI的Normalized
    Arduino可穿戴教程之第一个程序——选择端口(三)
    缩略图信息提取工具vinetto
    jquery的defer
    toDo
    瀑布流案例
    js基本知识6
    js基本知识5
  • 原文地址:https://www.cnblogs.com/lykxbg/p/15212575.html
Copyright © 2020-2023  润新知