Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
33 lines (28 loc) · 876 Bytes

File metadata and controls

33 lines (28 loc) · 876 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
29
30
31
32
33
package LeetCode.array;
import java.util.HashMap;
/**
* 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.
* @author Administrator
*
*/
public class TwoSum {
public TwoSum() {
// TODO Auto-generated constructor stub
}
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();
int[] result = new int[2];
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(target - nums[i])) {
result[1] = i;
result[0] = map.get(target - nums[i]);
break;
}
map.put(nums[i], i);
}
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.