My solution, intelligible brutal force(O(n2), O(1)) but not fast:
1 2 3 4 5 6
classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: for i, num1 in enumerate(nums): for j, num2 in enumerate(nums[i + 1:]): if num1 + num2 == target: return [i, j + i + 1]
OR
1 2 3 4 5 6
classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] + nums[j] == target: return [i, j]
classSolution: deftwoSum(self, nums, target): # h represents hash table, in py, it's simple dictionary here # let it be twoSum([5, 2, 1], 3) # 1st --> h = {5:0} # 2nd --> h = {5: 0, 2: 1} # 3rd --> return [1,2] h = {} for i, num in enumerate(nums): # direct lookup if exists in h if target - num in h: return [h[target - num], i] # store numbers and indices if not in h h[num] = i
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:
1 2 3 4 5 6 7
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
Solution
I tried this:
1 2 3 4 5 6 7 8 9 10 11 12
classSolution(object): defthreeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res = [] for i, num1 in enumerate(nums): for j, num2 in enumerate(nums[i+1:]): if - num1 - num2 in nums[i+1+j+1:]: res.append(tuple(sorted([num1, num2, - num1 - num2]))) return [list(j) for j in set(res)]
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example 1:
1 2 3
Input: nums = [-1,2,1,-4], target = 1 Output: 2 Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Constraints:
3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defthreeSumClosest(self, nums: List[int], target: int) -> int: ls = len(nums) sort_nums = sorted(nums) res = nums[0] + nums[1] + nums[2] for i in range(ls - 2): j, k = i + 1, ls - 1 while j < k: temp = sort_nums[i] + sort_nums[j] + sort_nums[k] if abs(target - temp) < abs(target - res): res = temp if temp < target: j += 1 else: k -= 1 return res
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 = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
The solution set must not contain duplicate quadruplets.
Example:
1 2 3 4 5 6 7 8
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13
Input: A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2]
classSolution: deffourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int: AB = collections.Counter(a+b for a in A for b in B) return sum(AB[-c-d] for c in C for d in D)