赞
踩
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.
现给一个n个整数的数组,找找看其中有没有三个数加起来为0。找到其中所有的不同的所有组,每组由三个数构成,并且三个数之和为0
Note:
The solution set must not contain duplicate triplets.
最右的结果一定是没有的重复组合的集合
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2:
Input: nums = []
Output: []
Example 3:
Input: nums = [0]
Output: []
Constraints:(限制)
0 <= nums.length <= 3000,数组的长度是在3000个以内
-105 <= nums[i] <= 105,数组的元素大小在±105之间
注意:
*三种循环,列出所有的三元素组合,判定是否相加为0
* 判定最后的组成的组合是否在集合中,不在就加入
class Solution(object): def threeSum(self, nums): result = set() result2 = list() if len(nums)>=3: for i in range(len(nums)-2): for j in range(i + 1,len(nums)- 1): for k in range(j + 1,len(nums)): if(nums[i] + nums[j] + nums[k] == 0): result.add(tuple(sorted((nums[i],nums[j],nums[k])))) for i in range(len(result)): result2.append(list(result.pop())) return result2 else: return [] if __name__ == '__main__': test = Solution() nums = [-1,0,1,2,-1,-4] print(type(nums)) print(test.threeSum(nums))
python中使用类创建对象,要在类名后面加上括号,不加会出现如下的异常。没有对对象进行初始化。
集合中的元素有要求,必须是不可变的数据结构,元素,字符串等;不可以以可变的数据结构作为其中的元素,如列表,字典。
元素相同,但是顺序不同的元组仍旧是不同的元组
图示
def threeSum(self,nums): result = list() if len(nums) >= 3: # 将原来的数组进行排序,屏蔽重复的数字的影响 nums.sort() # 分为两重遍历,最外侧遍历是确定三数相加的第一个位置,并将问题简化为2Sum,第二层遍历是确定解决2Sum的问题 i = 0 while i < len(nums): left = i + 1 right = len(nums) - 1 while left < right: if nums[left] + nums[right] == 0 - nums[i]: result.append([nums[i], nums[left], nums[right]]) temp = 1 while left + temp < len(nums) and nums[left + temp] == nums[left]: temp += 1 left = left + temp temp = 1 while nums[right - temp] == nums[right]: temp += 1 right = right - temp elif nums[left] + nums[right] > 0 - nums[i]: temp = 1 while nums[right - temp] == nums[right]: temp += 1 right = right - temp else: temp = 1 while left + temp < len(nums) and nums[left + temp] == nums[left]: temp += 1 left = left + temp temp = 1 while i + temp < len(nums) and nums[i + temp] == nums[i]: temp += 1 i += temp return result else: return []
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: res = [] nums.sort() for i, a in enumerate(nums): if i > 0 and a == nums[i - 1]: continue # 迭代之后,先判定和之前的值是否相同,如果相同直接跳过,进行下一次循环。 l, r = i + 1, len(nums) - 1 while l < r: threeSum = a + nums[l] + nums[r] if threeSum > 0: r -= 1 elif threeSum < 0: l += 1 # 如果遇见的是相同的,没有必要在进行判定是否相同,相同并不会起到缩小或者增加的作用 else: res.append([a, nums[l], nums[r]]) l += 1 while nums[l] == nums[l - 1] and l < r: l += 1 # 没有必要两边同时缩小,仅仅需要改变一边,另外一边会自动平衡 return res
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
... print i, element
...
0 one
1 two
2 three
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。