LeetCode第四天

leetcode 第四天

2018年1月4日

15.(628)Maximum Product of Three Numbers

JAVA
class Solution {
    public int maximumProduct(int[] nums) {
        Arrays.sort(nums);
        return (nums[0]*nums[1]*nums[nums.length-1])>(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3])?(nums[0]*nums[1]*nums[nums.length-1]):(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3]);
    }
}

16.(628)==Maximum Product of Three Numbers==

JAVA
class Solution {
    public int thirdMax(int[] nums) {
        Integer first = null ,second = null,third = null;
        for(Integer n : nums){
            if(n.equals(first)||n.equals(second)||n.equals(third)) continue;
            if(first==null||n>first){
                third = second;
                second = first;
                first = n;
            }else if(second == null||n>second){
                third = second;
                second = n;
            }else if(third == null||n>third){
                third = n;
            }

        }

        return third == null?first:third;
    }

}

17.(643) Maximum Average Subarray I

JAVA
class Solution {
    public double findMaxAverage(int[] nums, int k) {
        double window = 0;
        double maxAvg = 0;
        for(int i =0;i<k;i++){
            window +=nums[i];
            maxAvg = window;
        }

        for(int i = k;i<nums.length;i++){
            window = window+nums[i]-nums[i-k];
            maxAvg = Math.max(maxAvg,window);

        }

        return maxAvg/k;
    }
}

18.(448) Find All Numbers Disappeared in an Array

JAVA
class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> result = new ArrayList<Integer>();
        for(int i =0;i<nums.length;i++){

            nums[Math.abs(nums[i])-1] = -1 * Math.abs(nums[Math.abs(nums[i])-1]);
        }
        for(int i =0;i<nums.length;i++){
            if(nums[i]>0)
                result.add(i+1);
        }
        return result;
    }
}

19.(485) Max Consecutive Ones

JAVA
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int max = 0;
        int times = 0;
        for(int i =0;i<nums.length;i++){
            if(nums[i]==1){
                times++;
                max = Math.max(max,times);
            }else{
                times = 0;
            }
        }
        return max;
    }
}

20.(88) Merge Sorted Array

JAVA
class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        while(n > 0){
            if(m>0)
                nums1[n+m-1] = nums1[m-1]>nums2[n-1]?nums1[--m]:nums2[--n];
            else
                nums1[n-1] = nums2[--n];
        }
    }
}

21.(605) Can Place Flowers

JAVA
class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int count = 0;
        for(int i =0;i<flowerbed.length;i++){
            if(flowerbed[i]==0&&(i==0||flowerbed[i-1]==0)&&(i==flowerbed.length-1||flowerbed[i+1]==0)){
                count++;
                flowerbed[i]=1;
                if(count==n)
                    return true;
            }
        }
        return count >= n;
    }
}

原文地址:https://www.cnblogs.com/guoyaohua/p/8196716.html

时间: 2024-08-06 20:50:23

LeetCode第四天的相关文章

LeetCode第四题,Add Two Numbers

题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6

LeetCode:四数之和【18】

LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为:[ [-1, 0, 0, 1], [-2, -1, 1, 2]

[LeetCode] 4Sum 四数之和

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order.

LeetCode 18. 四数之和(4Sum)

题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]

Leetcode 454.四数相加II

四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 .所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 . 例如: 输入: A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2] 输出: 2

LeetCode第四十八题-旋转图像

Rotate Image 问题简介: 给定一个可以用图像形容的n x n 的2D矩阵,将这个图像即数组顺时针旋转90度 注: 只能更改给定的数组,不允许通过另一个数组或集合来解决 举例: 1: 给定数组:matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 旋转后结果: [ [7,4,1], [8,5,2], [9,6,3] ] 2: 给定数组:matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,

leetcode 454. 四数相加 II java

题目: 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 .所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 . 例如: 输入:A = [ 1, 2]B = [-2,-1]C = [-1, 2]D = [ 0, 2] 输出:2 解释:两个元组如

LeetCode第四题Median of Two Sorted Arrays解法

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). example: nums1 = [1, 3] nums2 = [2] The median is 2.0 简单来说,这道题的要求就是求两个数组的中位数,因

[Leetcode 18]四数之和 4 Sum

[题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note:The solution set must not contain d