每日三题-盛最多水的容器、三数之和、移动零

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 每日三题-盛最多水的容器、三数之和、移动零

盛最多水的容器


78c322f2fb6945e5a140ab081486fcc0.png

解法一

双指针

left指向左边,right指向右边

如果num[left] <= num[right] 那么left++反之righht–

因为盛水靠矮的一方

class Solution {
    public int maxArea(int[] height) {
        int len = height.length;
        if(len <= 1) return 0;
        int left = 0;
        int right = len-1;
        int res = 0;
        while(left < right){
            res = Math.max(res,Math.min(height[left],height[right])*(right-left));
            if(height[left] <= height[right]) left++;
            else if(height[left] > height[right]) right--;
        }
        return res;
    }
}


三数之和


2a3003694dd149a6bc095f041b8cb31a.png

解法一

双指针

先进行排序,然后遍历当前num[i] + num[left] +num[right] 与 0 的大小

如果大于0所以right–;

如果小于0所以left++

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        int len = nums.length;
        List<List<Integer>> list = new ArrayList<>();
        if(len < 3) return list;
        Arrays.sort(nums);
        for(int i = 0;i < len;i++){
            if(i > 0 && nums[i-1] == nums[i]) continue;
            int left = i + 1;
            int right = len-1;
            while(left < right){
                if(nums[i] + nums[left] + nums[right] == 0){
                    // System.out.println(i+"=="+left+"=="+right);
                    List<Integer> t = new ArrayList<>();
                    t.add(nums[i]);
                    t.add(nums[left]);
                    t.add(nums[right]);
                    list.add(t);
                    while(left < right && nums[left+1] == nums[left]) left++;
                    while(left < right && nums[right-1] == nums[right]) right--;
                    left++;
                    right--;
                }else if(nums[i] + nums[left] + nums[right] > 0){
                    right--;
                }else left ++;
            }
        }
        return list;
    }
}

移动零


c90c778acc1948099b7d59377aecd353.png

解法一

class Solution {
    public void moveZeroes(int[] nums) {
        int len = nums.length;
        if(len == 0) return;
        int right = 0;
        for(int i = 0;i < len;i++){
            if(nums[i] != 0){
                nums[right++] = nums[i];
            }
        }
        for(int i = right;i < len;i++){
            nums[i] = 0;
        }
        return;
    }
}
相关文章
|
3天前
|
存储 容器
LeetCode刷题---11. 盛最多水的容器(双指针-对撞指针)
LeetCode刷题---11. 盛最多水的容器(双指针-对撞指针)
|
7月前
|
算法 容器
【算法专题突破】双指针 - 盛最多水的容器(4)
【算法专题突破】双指针 - 盛最多水的容器(4)
20 0
|
7月前
|
算法 测试技术 容器
【算法挨揍日记】day02——双指针算法_快乐数、盛最多水的容器
题目: 编写一个算法来判断一个数 n 是不是快乐数。 「快乐数」 定义为:
39 0
|
3天前
|
容器
leetcode代码记录(盛最多水的容器
leetcode代码记录(盛最多水的容器
9 1
|
3天前
|
容器
11.盛最多水的容器
11.盛最多水的容器
13 0
|
3天前
|
容器
【力扣】11. 盛最多水的容器
【力扣】11. 盛最多水的容器
|
3天前
|
容器
LeetCode题:11. 盛最多水的容器
LeetCode题:11. 盛最多水的容器
16 0
|
3天前
|
Python 容器 人工智能
Python每日一练(20230402) 对称二叉树、全排列、盛最多水的容器
Python每日一练(20230402) 对称二叉树、全排列、盛最多水的容器
29 0
Python每日一练(20230402) 对称二叉树、全排列、盛最多水的容器
|
3天前
|
人工智能 容器
leetcode-11:盛最多水的容器
leetcode-11:盛最多水的容器
29 0
|
3天前
|
存储 算法 Java
Leetcode算法系列| 11. 盛最多水的容器
Leetcode算法系列| 11. 盛最多水的容器