Article Directory:
- topic
- A script and notes
- A script logic
- Two scripts and notes
- Scripts two logic
topic:
Given an array nums n comprises integers, it determines whether there are three elements a, b, c nums such that a + b + c = 0? All the conditions are not satisfied to find duplicate triples.
Note: The answer can not contain duplicate triples.
For example, given the array nums = [-1, 0, 1, 2, -1, -4],
Meet the requirements set of triples as:
[
[-1, 0, 1],
[-1, -1, 2]
]
And a script Note: [Time: time out, brute force]
class Solution: DEF threeSum (Self, the nums: List [int]) -> List [List [int]]: N1 = # 1 is defined as a variable value 1 for continuously slicing list nums.sort () # due a list of elements is not repeated List1 = [] # define a storing results empty list for I in the nums: # traversing the list of elements nums2 = the nums [N1:] # list slicing the while nums2: # traversing sections element J = nums2.pop (0) # pop the first element slice list if- (I + J) in nums2: # slice list remaining list elements traversing IF [I, J, - (I + J)] Not in List1: # select sum element 0, and this element is not stored results list list1.append ([I, J, - (I + J)]) # If met, the adding elements N1 + N1 = # increment. 1 return (List1) # after completion of traverse, return results
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: n1 = 1 nums.sort() list1 = [] for i in nums: nums2 = nums[n1:] while nums2: j = nums2.pop(0) if -(i+j) in nums2: if [i,j,-(i+j)] not in list1: list1.append([i,j,-(i+j)]) n1 += 1 return(list1)
A script logic:
- Through all the ternary compositions will not be repeated, and the ternary element, and 0 is added to the new list
- This is a brute force method, the result is feasible, but then it leetcode in overtime
Two scripts and notes:
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: nums.sort() res, k = [], 0 for k in range(len(nums) - 2): if nums[k] > 0: break # 1. because of j > i > k. if k > 0 and nums[k] == nums[k - 1]: continue # 2. skip the same `nums[k]`. i, j = k + 1, len(nums) - 1 while i < j: # 3. double pointer s = nums[k] + nums[i] + nums[j] if s < 0: i += 1 while i < j and nums[i] == nums[i - 1]: i += 1 elif s > 0: j -= 1 while i < j and nums[j] == nums[j + 1]: j -= 1 else: res.append([nums[k], nums[i], nums[j]]) i += 1 j -= 1 while i < j and nums[i] == nums[i - 1]: i += 1 while i < j and nums[j] == nums[j + 1]: j -= 1 return res
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: nums.sort() res, k = [], 0 for k in range(len(nums) - 2): if nums[k] > 0: break # 1. because of j > i > k. if k > 0 and nums[k] == nums[k - 1]: continue # 2. skip the same `nums[k]`. i, j = k + 1, len(nums) - 1 while i < j: # 3. double pointer s = nums[k] + nums[i] + nums[j] if s < 0: i += 1 while i < j and nums[i] == nums[i - 1]: i += 1 elif s > 0: j -= 1 while i < j and nums[j] == nums[j + 1]: j -= 1 else: res.append([nums[k], nums[i], nums[j]]) i += 1 j -= 1 while i < j and nums[i] == nums[i - 1]: i += 1 while i < j and nums[j] == nums[j + 1]: j -= 1 return res
Script two and logic:
- Answers to this question is necessarily need to traverse all the elements, this script is more careful handling of the value list, will weed out those elements do not need to reduce the number of traversal