-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
28 lines (27 loc) · 865 Bytes
/
TwoSum.java
File metadata and controls
28 lines (27 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Map;
import java.util.HashMap;
/**
* 给定一个数组和一个target求出数组中两个数相加=target的数在数组中的位置(一个值只可以使用一次)
* leetCode one
*/
class TwoSum{
public static int[] twosum(int[] num,int target){
Map<Integer,Integer> map = new HashMap<>();
int[] result = new int[2];
for(int i=0; i<num.length; i++){
if(map.containsKey(target-num[i])){
result[0] = i;
result[1] = map.get(target-num[i]);
}else{
map.put(num[i], i);
}
}
return result;
}
public static void main(String[] args) {
int[] nums = {2, 7, 11, 13};
int target = 13;
int[] result = twosum(nums,target);
System.out.println(result[0]+" " +result[1]);
}
}