【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());
    }

}

相关推荐
有时间要学习2 分钟前
【无标题】
算法
re林檎3 分钟前
算法札记——5.15
算法
鱼子星_8 分钟前
【数据结构与算法】OJ题目详解(一)-单链表:从易到难的面试OJ题目
c语言·数据结构·算法·链表·面试·职场和发展
人道领域9 分钟前
【LeetCode刷题日记】递归与回溯实战 257.二叉树的所有路径——一篇文章彻底搞懂回溯
开发语言·python·算法·leetcode
ulias21210 分钟前
leetcode热题 - 7
数据结构·算法·leetcode
吃好睡好便好12 分钟前
在Matlab中用sphere( )函数绘制球面图
开发语言·前端·javascript·学习·算法·matlab·信息可视化
图码13 分钟前
矩阵中的“对角线强迫症”:如何优雅地判断Toeplitz矩阵?
数据结构·c++·线性代数·算法·青少年编程·矩阵
lynnlovemin13 分钟前
二分查找与二分答案算法详解(基于C++实现)
c语言·开发语言·算法·二分查找·二分答案
瑞行AI16 分钟前
一套数据格式框架搞定大模型微调和对齐训练
算法·语言模型
玛卡巴卡ldf16 分钟前
【LeetCode 手撕算法】(动态规划)爬楼梯、杨辉三角、打家劫舍、完全平方数、零钱兑换、单词拆分、最长递增子序列、乘积最大子数组、分割等和子集
java·数据结构·算法·leetcode·动态规划·力扣