-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathTwoSum.java
35 lines (29 loc) · 1.2 KB
/
TwoSum.java
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
34
35
/*
Author: Andy, [email protected]
Date: Dec 13, 2014
Problem: Two Sum
Difficulty: Medium
Source: https://oj.leetcode.com/problems/two-sum/
Notes:
Given an array of integers, 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.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
Solution: 1. Hash table. O(n)
Note: Hash Table solution has been updated. In case that the two elements are the same,
all the indices should be stored in the map.
*/
public class Solution {
public int[] twoSum(int[] numbers, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; ++i) {
int b = target - numbers[i];
if (map.get(b) != null) {
return new int[]{map.get(b),i};
} else map.put(numbers[i],i);
}
return null;
}
}