经典的2sum,3sum,4sum极其变体题目总结,需要熟记。
Leetcode 1. Two Sum
Question:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Explanation:
用HashMap存差值,如果之后找到了立即返回两个index。
Code:
Leetcode 167. Two Sum II - Input array is sorted
Question:
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution and you may not use the same element twice.
Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2
Explanation:
Array是sorted,所以用两指针,找到符合条件的两个index。
Code:
Leetcode 170. Two Sum III - Data structure design
Question:
Design and implement a TwoSum class. It should support the following operations: add and find.
add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is equal to the value.
Explanation:
一道典型的Trade off, 如果要add为O(n), 则find为 o(1),反之。
Code:
Leetcode 15. 3Sum
Question:
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.
Explanation:
先排序,遍历其中每个值,找到target减去后的值,按照two sum in sorted来做就行了。要注意skip相同值,不能重复放。
Code:
Leetcode 16. 3Sum Closest
Question:
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.
Explanation:
同样的用3sum的方法做,只不过要多一个值来存储最小的diff和最靠近的sum。
Code:
Leetcode 259. 3Sum Smaller
Question:
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.
Return 2. Because there are two triplets which sums are less than 2:
Explanation:
先排序,遍历其中每个值,找到target减去后的值,按照two sum in sorted来做就行了,但是要注意,因为只要算比它小的个数,所以注意指针如何移动,并且如何count。
Code:
Leetcode 18. 4Sum
Question:
Given an array S of n integers, are there elements a, b, c, and d in S 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.
Explanation:
在3Sum上面套一层就行了。吐槽无力。
Code:
Leetcode 454. 4Sum II
Question:
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.
Explanation:
O(N^2)+Hashmap. Boring,下一题。