-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargetSum.java
36 lines (34 loc) · 871 Bytes
/
TargetSum.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
36
package google;
import java.util.HashSet;
public class TargetSum {
public static boolean TargetExist(int[] input, int target){
int len = input.length-1;int i=0;
while(i!=len){
if((input[i] + input[len]) == target){
return true;
}else if ((input[i] + input[len]) > target){
len--;
}else if((input[i] + input[len]) < target){
i++;
}
}
return false;
}
public static boolean UnsortedTargetExist(int[] input, int target){
int len = input.length-1;int i=0;HashSet<Integer> compliment = new HashSet<Integer>();
for(i=0; i<=len; i++){
if(compliment.contains(input[i])){
return true;
}else{
compliment.add(target-input[i]);
}
}
return false;
}
// Tester
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,10,0,4,4,8};
System.out.println(UnsortedTargetExist(a,17));
}
}