-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1.java
More file actions
38 lines (34 loc) · 1.06 KB
/
prblm1.java
File metadata and controls
38 lines (34 loc) · 1.06 KB
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
36
37
38
import java.util.*;
public class prblm1 {
public static void main(String[] args) {
int[] nums = {2,7,11,15};
int target = 9;
System.out.println(Arrays.toString(new prblm1().twoSum(nums,target)));
int[] nums2 = {3,2,4};
int target2 = 6;
System.out.println(Arrays.toString(new prblm1().twoSum(nums2,target2)));
}
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i<nums.length; i++){
for(int j = i+1; j<nums.length; j++){
if(nums[i] + nums[j] == target){
int a[] = {i,j};
return a;
}
}
}
return null;
}
public int[] twoSum2(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
int y = target - x;
if(map.containsKey(y)){
return new int[] {map.get(y), i};
}
map.put(x, i);
}
return null;
}
}