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