forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo-Sum.java
More file actions
25 lines (24 loc) · 699 Bytes
/
Two-Sum.java
File metadata and controls
25 lines (24 loc) · 699 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
class Solution {
public int[] twoSum(int[] nums, int target) {
int arr[] = new int[2];
Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; ++i) {
mp.put(nums[i], i);
}
for(int j = 0; j < nums.length; ++j) {
int comp = target - nums[j];
if(mp.containsKey(comp)) {
int val = mp.get(comp);
if(val == j) {
continue;
}
else {
arr[0] = j;
arr[1] = val;
break;
}
}
}
return arr;
}
}