Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target。You may assume that each input would have exactly
one solution, and you may not use the same element twice.
给定一个总和target,找出两个数总和等于target。两个坐标不能相同。
此类题目需要涉及到的问题:
1)u can use two for loops,time comlexity is n squared
-
u can sort the array using quick sort or merge sort, time comlexity is the logn power of n
-
if two numbers in array are equal,we can count the same numbers, to find the two numbers sum is target.
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> mp=new HashMap(); for(int i=0;i<nums.length;i++){ mp.put(nums[i],i); } for(int i=0;i<nums.length;i++){ if(mp.get(target-nums[i])!=null && mp.get(target-nums[i])!=i){ return new int[]{i,mp.get(target-nums[i])}; } } return null;
}}
Similar question:
167. Two Sum II - Input array is sorted
Given an array of integers that is already
, 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.
Note:
-
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.class Solution { public int[] twoSum(int[] numbers, int target) { int i=0,j=numbers.length-1; while(i<=j){ if(numbers[i]+numbers[j]<target){ i++; }else if(numbers[i]+numbers[j]>target){ j--; }else{ return new int[]{i+1,j+1}; } } return null;
}}
Two Sum III - Data structure design
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.
For example:
add(1); add(3); add(5);
find(4) -> true
find(7) -> false
注意:两个元素相同,需要用记录个数方式,防止被遗漏。若是采用记录下标,会遗漏,相同的数目只会保留一个。
public class TwoSum{
private Map<Integer,Integer> mp=new HashMap();
public void add(Integer a){
if(mp.get(a)==null){
mp.put(a,1);
}else{
mp.put(a,mp.get(a)+1);
}
}
public boolean find(int target){
for(int num:mp.keyset()){
int sub=target-num;
if(sub==num&& mp.get(num)<2){
continue;
}
if(mp.get(sub)>=1){
return true;
}
}
return false;
}
}
560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
Example 1:
Input:nums = [1,1,1], k = 2
Output: 2
there are three solutions:
one:
class Solution {
public int subarraySum(int[] nums, int k) {
int result=0;
for(int i=0;i<nums.length;i++){
int sum=nums[i];
if(nums[i]==k){
result++;
}
for(int j=i+1;j<nums.length;j++){
sum+=nums[j];
if(sum==k){
result++;
}
}
}
return result;
}
}
two:
class Solution {
public int subarraySum(int[] nums, int k) {
int result=0;
int[] sumArr=new int[nums.length];
int sum=0;
for(int i=0;i<nums.length;i++){
sum+=nums[i];
sumArr[i]=sum;
}
for(int i=0;i<sumArr.length;i++){
if(sumArr[i]==k){
result++;
}
for(int j=i-1;j>=0;j--){
if(sumArr[i]-sumArr[j]==k){
result++;
}
}
}
return result;
}
}
three:
using map record the sum
class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer,Integer> mp=new HashMap();
mp.put(0,1);
int sum=0,result=0;
for(int i=0;i<nums.length;i++){
sum+=nums[i];
if(mp.get(k-sum)!=null){
result+=mp.get(k-sum);
}
if(mp.get(sum)==null){
mp.put(sum,1);
}else{
mp.put(sum,mp.get(sum)+1);
}
}
return sum;
}
}
653. Two Sum IV - Input is a BST
Given the root of a Binary Search Tree and a target number k, return
.
Example 1:

Input: root = [5,3,6,2,4,null,7], k = 9
Output: true
Example 2:

Input: root = [5,3,6,2,4,null,7], k = 28
Output: false
Example 3:
Input: root = [2,1,3], k = 4
Output: true
Example 4:
Input: root = [2,1,3], k = 1
Output: false
Example 5:
Input: root = [2,1,3], k = 3
Output: true
Constraints:
-
The number of nodes in the tree is in the range
[1, 10<sup>4</sup>]. -
-10<sup>4</sup> <= Node.val <= 10<sup>4</sup> -
rootis guaranteed to be a valid binary search tree. -
-10<sup>5</sup> <= k <= 10<sup>5</sup>class Solution { public boolean findTarget(TreeNode root, int k) { Set st=new HashSet(); return bfs(root,st,k);
} boolean bfs(TreeNode root,Set<Integer> st,int k){ if(root==null) return false; if(st.contains(k-root.val)) return true; st.add(root.val); return bfs(root.left,st,k) || bfs(root.right,st,k); }}
另一种解法
class Solution {
public boolean findTarget(TreeNode root, int k) {
List<Integer> list=new ArrayList();
bfs(root,list);
for(int i=0,j=list.size()-1;i<j;){
if(list.get(i)+list.get(j)<k){
i++;
}else if(list.get(i)+list.get(j)>k){
j--;
}else{
return true;
} }
return false;
}
void bfs(TreeNode root,List<Integer> list){
//left mid right
if(root==null) return ;
if(root.left!=null) bfs(root.left,list);
list.add(root.val);
if(root.right!=null) bfs(root.right,list);
}
}
1099. Two Sum Less Than K
Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, jexist satisfying this equation, return -1.
Example 1:
Input: A = [34,23,1,24,75,33,54,8], K = 60
Output: 58
Explanation:
We can use 34 and 24 to sum 58 which is less than 60.
Example 2:
Input: A = [10,20,30], K = 15
Output: -1
Explanation:
In this case it's not possible to get a pair sum less that 15.
Note:
-
1 <= A.length <= 100 -
1 <= A[i] <= 1000 -
1 <= K <= 2000class Solution { public int twoSumLessThanK(int[] A, int K) { if(A==null || A.length==0) return =-1; Array.sort(A); int res=-1; for(int i=0,j=A.length-1;i<j;){ if(A[i]+A[j]>=k) j--; else if (A[i]+A[j]<k){ res=Math.max(res,A[i]+A[j]); i++; } } return res; }
}
15. 3Sum
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.
Notice that 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-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup>
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list=new ArrayList();
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
if(i>0 && nums[i]==nums[i-1]) continue;
int sum=0-nums[i];
int j=i+1,k=nums.length-1;
while(j<k){
if(nums[j]+nums[k]==sum){
list.add(Arrays.asList(nums[i],nums[j],nums[k]));
while(j<k && nums[j]==nums[j+1] )j++;
while(j<k && nums[k]==nums[k-1] )k--;
j++;k--;
}else if(nums[j]+nums[k]<sum){
j++;
}else{
k--;
}
}
}
return list;
}
}