@@ -22,44 +22,44 @@ public int getIndex() {
22
22
return index ;
23
23
}
24
24
25
- public void setIndex (int index ) {
26
- this .index = index ;
27
- }
25
+ public void setIndex (int index ) {
26
+ this .index = index ;
27
+ }
28
28
29
- public int getValue () {
30
- return value ;
31
- }
29
+ public int getValue () {
30
+ return value ;
31
+ }
32
32
33
- public void setValue (int value ) {
34
- this .value = value ;
35
- }
33
+ public void setValue (int value ) {
34
+ this .value = value ;
36
35
}
36
+ }
37
37
38
- public int [] twoSum (int [] numbers , int target ) {
39
- Num [] newArray = new Num [numbers .length ];
40
- for (int i = 0 ; i < numbers .length ; i ++) {
41
- newArray [i ] = new Num (numbers [i ], i );
38
+ public int [] twoSum (int [] numbers , int target ) {
39
+ Num [] newArray = new Num [numbers .length ];
40
+ for (int i = 0 ; i < numbers .length ; i ++) {
41
+ newArray [i ] = new Num (numbers [i ], i );
42
42
}
43
- Arrays .sort (newArray , new Comparator <Num >() {
44
- public int compare (Num n1 , Num n2 ) {
45
- if (n1 .getValue () == n2 .getValue ()) return 0 ;
46
- if (n1 .getValue () > n2 .getValue ()) return 1 ;
47
- return -1 ;
48
- }
49
- });
50
- int [] result = new int [2 ];
51
- int i = 0 , j = numbers .length - 1 ;
43
+ Arrays .sort (newArray , new Comparator <Num >() {
44
+ public int compare (Num n1 , Num n2 ) {
45
+ if (n1 .getValue () == n2 .getValue ()) return 0 ;
46
+ if (n1 .getValue () > n2 .getValue ()) return 1 ;
47
+ return -1 ;
48
+ }
49
+ });
50
+ int [] result = new int [2 ];
51
+ int i = 0 , j = numbers .length - 1 ;
52
52
while (i < j ) {
53
- int tmp = newArray [i ].getValue () + newArray [j ].getValue ();
54
- if (tmp == target ) {
55
- result [0 ] = Math .min (newArray [i ].getIndex () + 1 , newArray [j ].getIndex () + 1 );
53
+ int tmp = newArray [i ].getValue () + newArray [j ].getValue ();
54
+ if (tmp == target ) {
55
+ result [0 ] = Math .min (newArray [i ].getIndex () + 1 , newArray [j ].getIndex () + 1 );
56
56
result [1 ] = Math .max (newArray [i ].getIndex () + 1 , newArray [j ].getIndex () + 1 );
57
- break ;
58
- } else if (tmp > target ) {
59
- j --;
60
- } else {
61
- i ++;
62
- }
57
+ break ;
58
+ } else if (tmp > target ) {
59
+ j --;
60
+ } else {
61
+ i ++;
62
+ }
63
63
}
64
64
return result ;
65
65
}
0 commit comments