力扣 ——3Sum python (三数之和)实现

题目描述:

中文:

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

英文:

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.

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        if len(nums) < 3:
            return result
        nums.sort()
        for i in range(0, len(nums)):
            j = i + 1
            k = len(nums) - 1
            if i > 0 and nums[i] == nums[i - 1]:
                continue  #
            while j < k:
                sum = nums[i] + nums[j] + nums[k]
                if sum == 0:
                    result.append((nums[i], nums[j], nums[k]))
                    k -= 1
                    j += 1
                    while(nums[j] == nums[j - 1] and nums[k] == nums[k + 1] and j < k):
                        j += 1
                elif sum > 0:
                    k -= 1
                    while(nums[k] == nums[k + 1] and j < k):
                        k -= 1
                else:
                    j += 1
                    while(nums[j] == nums[j - 1] and j < k):
                        j += 1
        return list(set(result))

题目来源:力扣

原文地址:https://www.cnblogs.com/spp666/p/11552389.html

时间: 2024-08-05 03:41:37

力扣 ——3Sum python (三数之和)实现的相关文章

python三数之和

给定一个包含 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(object): def threeSum(self, nums): """ :type

力扣 ——4Sum (四数之和)python 实现

题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 英文: Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d

LeetCode 15. 3Sum(三数之和)

Given an array S of n integers, are there elements a, b, c in S 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. For example, given array S = [-1,

259 [LeetCode] 3Sum Smaller 三数之和较小值

题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. For example, given nums = [-2, 0, 1, 3], and target = 2.

力扣算法初级——两数之和

相关知识点: 1.c++计算数组的大小:使用“数组名+size()” 2.c++返回多个数据的方法:return {a,b,……} 3.哈希表:散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映射函数叫做散列函数,存放记录的数组叫做散列表. 原文地址:https://www.cnblogs.com/181118ljh123/p/11650082.html

【力扣1】两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 一.暴力解法(两层循环) static int[] TwoSum(int[] nums, int target) { for

[LeetCode] 16. 3Sum Closest 最近三数之和

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

[LeetCode] 3Sum Closest 最近三数之和

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

LeetCode OJ:3Sum Closest(最接近的三数之和)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2