首页 > 其他 > 详细

LeetCode——15. 三数之和

时间:2020-02-03 12:05:59      阅读:64      评论:0      收藏:0      [点我收藏+]

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

解题方案

排序 + 双指针

本题的难点在于如何去除重复解。

算法流程:

  1. 特判,对于数组长度 n,如果数组为 null 或者数组长度小于 3,返回[]。
  2. 对数组进行排序。
  3. 遍历排序后数组:
    若 nums[i]>0:因为已经排序好,所以后面不可能有三个数加和等于 0,直接返回结果。
    对于重复元素:跳过,避免出现重复解
    令左指针 L=i+1L=i+1,右指针 R=n-1R=n?1,当 L<RL<R 时,执行循环:
    当 nums[i]+nums[L]+nums[R]==0,执行循环,判断左界和右界是否和下一位置重复,去除重复解。并同时将 L,R移到下一位置,寻找新的解
    若和大于 0,说明 nums[R] 太大,R 左移
    若和小于 0,说明 nums[L] 太小,L 右移

复杂度分析

时间复杂度:O(n^2),数组排序O(NlogN),遍历数组 O(n),双指针遍历 O(n),总体 O(NlogN)+O(n)?O(n),O(n^2)
空间复杂度:O(1)

Python3

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        n=len(nums)
        res=[]
        if(not nums or n<3):
            return []
        nums.sort()
        res=[]
        for i in range(n):
            if(nums[i]>0):
                return res
            if(i>0 and nums[i]==nums[i-1]):
                continue
            L=i+1
            R=n-1
            while(L<R):
                if(nums[i]+nums[L]+nums[R]==0):
                    res.append([nums[i],nums[L],nums[R]])
                    while(L<R and nums[L]==nums[L+1]):
                        L=L+1
                    while(L<R and nums[R]==nums[R-1]):
                        R=R-1
                    L=L+1
                    R=R-1
                elif(nums[i]+nums[L]+nums[R]>0):
                    R=R-1
                else:
                    L=L+1
    return res

Java

class Solution {
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ans = new ArrayList();
        int len = nums.length;
        if(nums == null || len < 3) return ans;
        Arrays.sort(nums); // 排序
        for (int i = 0; i < len ; i++) {
            if(nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
            if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
            int L = i+1;
            int R = len-1;
            while(L < R){
                int sum = nums[i] + nums[L] + nums[R];
                if(sum == 0){
                    ans.add(Arrays.asList(nums[i],nums[L],nums[R]));
                    while (L<R && nums[L] == nums[L+1]) L++; // 去重
                    while (L<R && nums[R] == nums[R-1]) R--; // 去重
                    L++;
                    R--;
                }
                else if (sum < 0) L++;
                else if (sum > 0) R--;
            }
        }        
        return ans;
    }
}

C++双指针

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        if(nums.size() < 3) return res;
        sort(nums.begin(),nums.end()); //先排序
        for(vector<int>::iterator it = nums.begin(); it != nums.end()-2;){
            int tmp = *it;
            if(tmp > 0) break;
            int target = 0 - tmp;
            vector<int>::iterator left = it+1;
            vector<int>::iterator right = nums.end()-1;
            while(left < right){
                if(*right < 0) break;  //如果右边小于0,break
                if(*left + *right < target){
                    int v= *left;
                    while(left != right && *left == v) left++; //跳过相等的元素
                }else if(*left + *right > target){
                    int v= *right;
                    while(left != right && *right == v) right --;//跳过相等的元素
                }else{
                    vector<int> tmp_res{tmp,*left,*right}; //保存结果
                    res.push_back(tmp_res);
                    int v= *left;
                    while(left != right &&  *left == v) left++;//跳过相等的元素
                    v= *right;
                    while(left != right &&  *right == v) right --;//跳过相等的元素
                }  
            }
            while(it != nums.end()-2 && *it == tmp) it++;//跳过相等的元素
        }
        return res;
    }
};

LeetCode——15. 三数之和

原文:https://www.cnblogs.com/wwj99/p/12254960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!