File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ public class Sol35229 {
5+
6+ public int [] bruteForce (int [] nums , int target ) {
7+ for (int i = 0 ; i < nums .length ; i ++) {
8+ for (int j = i +1 ; j < nums .length ; j ++) {
9+ if (nums [i ]+nums [j ] == target ) {
10+ return new int []{i , j };
11+ }
12+ }
13+ }
14+ return new int [] {};
15+ }
16+
17+ public int [] hashTable (int [] nums , int target ) {
18+ Map <Integer , Integer > hashMap = new HashMap <>();
19+ for (int i = 0 ; i < nums .length ; i ++) {
20+ hashMap .put (nums [i ], i );
21+ }
22+ for (int i = 0 ; i < nums .length ; i ++) {
23+ int complement = target - nums [i ];
24+ if (hashMap .containsKey (complement ) && hashMap .get (complement ) != i ) {
25+ return new int []{i , hashMap .get (complement )};
26+ }
27+ }
28+ return new int [] {};
29+ }
30+ }
31+
You can’t perform that action at this time.
0 commit comments