leetcode 1. two sum [easy]

题目来源:https://leetcode.com/problems/two-sum/

题目难度:Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.HashMap;

public class {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap();

for (int i = 0; i < nums.length; i++) {
if (map.get(target - nums[i]) != null) {
return new int[] { map.get(target - nums[i]), i };
} else {
map.put(nums[i], i);
}
}

throw new IllegalArgumentException("No two sum solution");
}
}