【Hot100】LeetCode—15. 三数之和

目录

  • [1- 思路](#1- 思路)
    • [双指针法-三指针 定1+移动2](#双指针法-三指针 定1+移动2)
  • [2- 实现](#2- 实现)
    • [⭐15. 三数之和------题解思路](#⭐15. 三数之和——题解思路)
  • [3- ACM 实现](#3- ACM 实现)


1- 思路

双指针法-三指针 定1+移动2

  • 1- 先排序------>用于去重
  • 2- 遍历nums数组
    • 2.1 去重i:遇到 当 i 大于 0 时,ii-1 相等 continue
    • 2.2 定义 leftright 指针,利用 while 处理
    • 2.3 求和 sum,为三数和
    • 2.4 移动指针:如果 sum>0 移动 right,如果 sum<0 移动 left。否则 收集结果
    • 2.5 收集结果后去重 :leftright

2- 实现

⭐15. 三数之和------题解思路

java 复制代码
class Solution {

    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);

        for(int i = 0 ; i < nums.length;i++){
            if(i>0 && nums[i-1] == nums[i]){
                continue;
            }

            int left = i+1;
            int right = nums.length-1;

            while(left<right){
                int sum = nums[i] + nums[left] + nums[right];
                if(sum>0){
                    right--;
                }else if(sum<0){
                    left++;
                }else{
                    res.add(new ArrayList<>(Arrays.asList(nums[i],nums[left],nums[right])));
                    // 去重 left 和 right
                    while(left<right && nums[left] == nums[left+1]) left++;
                    while(left<right && nums[right] == nums[right-1]) right--;
                    left++;
                    right--;
                }
            }
        }
        return res;
    }
}

3- ACM 实现

java 复制代码
public class threeSum {


    public static List<List<Integer>> res = new ArrayList<>();
    public static List<List<Integer>> threeSum1(int[] nums){
        Arrays.sort(nums);


        for(int i = 0 ; i < nums.length;i++){
            // 2.1去重i
            if(i>0 && nums[i]==nums[i-1]){
                continue;
            }

            //2.双指针
            int left = i+1;
            int right = nums.length-1;
            while (left<right){
                int sum = nums[i] + nums[left] + nums[right];
                if(sum>0){
                    right--;
                }else if(sum<0){
                    left++;
                }else{
                    res.add(new ArrayList<>(Arrays.asList(nums[i],nums[left],nums[right])));
                    while(left<right && nums[left] == nums[left+1]) left++;
                    while (left<right && nums[right-1] == nums[right]) right--;
                    left++;
                    right--;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入数组长度");
        int n = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0 ; i < n ;i++){
            nums[i] = sc.nextInt();
        }

        System.out.println("结果是"+threeSum1(nums).toString());
    }

}

相关推荐
点云SLAM35 分钟前
二叉树算法详解和C++代码示例
数据结构·c++·算法·红黑树·二叉树算法
今天背单词了吗9808 小时前
算法学习笔记:19.牛顿迭代法——从原理到实战,涵盖 LeetCode 与考研 408 例题
笔记·学习·算法·牛顿迭代法
jdlxx_dongfangxing8 小时前
进制转换算法详解及应用
算法
why技术9 小时前
也是出息了,业务代码里面也用上算法了。
java·后端·算法
2501_9228955810 小时前
字符函数和字符串函数(下)- 暴力匹配算法
算法
IT信息技术学习圈11 小时前
算法核心知识复习:排序算法对比 + 递归与递推深度解析(根据GESP四级题目总结)
算法·排序算法
愚润求学11 小时前
【动态规划】01背包问题
c++·算法·leetcode·动态规划
会唱歌的小黄李11 小时前
【算法】贪心算法入门
算法·贪心算法
轻语呢喃12 小时前
每日LeetCode : 两数相加--链表操作与进位的经典处理
javascript·算法
钢铁男儿12 小时前
C# 接口(接口可以继承接口)
java·算法·c#