Python版[leetcode]15. 三数之和(难度中等)

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

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

示例:

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

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

这道题我感觉是有点难度的,一般都是2个数做比较,这次有3个数参与了,没有想到什么好的方法,后来直接找的参考

排序 + 双指针

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

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

原文地址:https://www.cnblogs.com/davidlidvd/p/12272780.html

时间: 2024-08-30 10:45:52

Python版[leetcode]15. 三数之和(难度中等)的相关文章

leetcode(15)三数之和+去重

三数之和 解题思路:排序+双指针 class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if(nums.length<=2){ return result; } List<Integer> item = null; A

LeetCode 15. 三数之和(3Sum)

题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ] 解题思路 首先对数组从小到大排序,从一个数开始遍历,若该数大于0,后面的数不可能与其相加和为0,所以跳过:否则该数可能是满足要求

Python版[leetcode]1. 两数之和(难度简单)

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 一开始我的想法是直接用2个for循环遍历nums,用当前数和当前数之后的所有数求和,如果和target相同就直接返回当前索引

leetcode 15.三数之和

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:[ [-1, 0, 1], [-1, -1, 2]] class Solution {public:    vector<vector<int>> threeSum(vector&l

[Leetcode 15]三数之和 3 Sum

[题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nu

15.三数之和——LeetCode

1 package threesum; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.List; 6 7 /** 8 * 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 9 * 注意:答案中不可以包含重复的三元组 10 * 11 * 主要的思想:如果采用暴力法的话,时间复杂

15. 三数之和(筛选数据)

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:[ [-1, 0, 1], [-1, -1, 2]] 1/** 2 * @param {number[]} nums 3 * @return {number[][]} 4 */ 5 6var th

Python版[leetcode]2. 两数相加(难度中等)

给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807 # Definition for singly-linked list.

leetcode新年病房暴乱康复计划 15. 三数之和 JS解法

var threeSum = function(nums) { var ans = []; var nums = nums.sort(function(a,b){return a - b}); var flag = 0; while(flag < nums.length - 2){ if (nums[flag] > 0){ break; } var l = flag + 1,r= nums.length - 1; while(l < r){ var res = nums[flag] +