File tree 4 files changed +162
-0
lines changed
4 files changed +162
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Note:
3
+ sort,nums[i]<=0, remove duplicates
4
+ */
5
+
6
+
7
+ public class Solution {
8
+ public List <List <Integer >> threeSum (int [] nums ) {
9
+ Arrays .sort (nums );
10
+ List < List <Integer > > results = new ArrayList < List <Integer > >();
11
+
12
+ for (int i =0 ;i < nums .length -2 && nums [i ]<=0 ;++i ){
13
+ int l = i +1 ;
14
+ int r = nums .length -1 ;
15
+ if (i >0 && nums [i ]==nums [i -1 ]){continue ;}
16
+
17
+ while (l <r ){
18
+ if (nums [l ]+nums [i ]+nums [r ]==0 ){
19
+ List <Integer > result = new ArrayList <Integer >();
20
+ result .add (nums [i ]);
21
+ result .add (nums [l ]);
22
+ result .add (nums [r ]);
23
+ results .add (result );
24
+ l ++;
25
+ r --;
26
+ while (l <r && nums [l ]==nums [l -1 ]) { l ++; }
27
+ while (l <r && nums [r ]==nums [r +1 ]) { r --; }
28
+ }else if (nums [l ]+nums [r ]+nums [i ]<0 ){
29
+ l ++;
30
+ }else {
31
+ r --;
32
+ }
33
+ }
34
+
35
+ }
36
+ return results ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ */
3
+
4
+ public class Solution {
5
+ public List <List <Integer >> fourSum (int [] nums , int target ) {
6
+
7
+ List <List <Integer > > results = new ArrayList <List <Integer > >();
8
+ Arrays .sort (nums );
9
+ for (int i =0 ; i < nums .length -3 ;i ++){
10
+ if (i >0 && nums [i ]==nums [i -1 ]){continue ;}
11
+ // Three sum
12
+ for (int j =i +1 ; j < nums .length -2 ;j ++ ){
13
+ if ( j >i +1 && nums [j ]==nums [j -1 ] ){ continue ; }
14
+ int l = j +1 ;
15
+ int r = nums .length -1 ;
16
+
17
+ while (l <r ){
18
+ if (nums [i ]+nums [j ]+nums [l ]+nums [r ] >target ){
19
+ r --;
20
+ }else if (nums [i ]+nums [j ]+nums [l ]+nums [r ]<target ){
21
+ l ++;
22
+ }else {
23
+ List <Integer > result = new ArrayList <Integer >();
24
+ result .add (nums [i ]);
25
+ result .add (nums [j ]);
26
+ result .add (nums [l ]);
27
+ result .add (nums [r ]);
28
+ results .add (result );
29
+ l ++;
30
+ r --;
31
+ while ( l <r && nums [l -1 ]==nums [l ]){ l ++; }
32
+ while ( l <r && nums [r +1 ]==nums [r ]){ r --; }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ return results ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ check at least 2 nodes
3
+ setup pre and current nodes
4
+ use next node to keep the safe place
5
+ when we moving nodes to next place.
6
+ */
7
+
8
+ public void reverseLinkList ( Node head ){
9
+ if ( head ==null || head .next ==null ){
10
+ return ;
11
+ }
12
+
13
+ Node pre = null ;
14
+ Node current = head ;
15
+ while ( current !=null ){
16
+ Node next = current .next ;
17
+ current .next =pre ;
18
+ pre = current ;
19
+ current = next ;
20
+ }
21
+ head = pre ;
22
+ }
23
+
24
+ // http://www.programmerinterview.com/index.php/data-structures/reverse-a-linked-list/
25
+
26
+ public void reverseLinkList (Node current ){
27
+
28
+ // for empty list
29
+ if (current ==null ){ return ; }
30
+
31
+ // stop case
32
+ if (current .next == null ){
33
+ Node head = current ;
34
+ return ;
35
+ }
36
+ reverseLinkList (current .next );
37
+ current .next .next = current ;
38
+ current .next =null ;
39
+ }
40
+
Original file line number Diff line number Diff line change
1
+ /* HasMap idea:
2
+ * if lookingFor is not in the HashMap
3
+ * store current number in the HasMap
4
+ * else
5
+ * return the nums[]
6
+ *
7
+ */
8
+
9
+ // 1
10
+ public class Solution {
11
+ public int [] twoSum (int [] nums , int target ) {
12
+
13
+ HashMap <Integer ,Integer > map = new HashMap ();
14
+ for ( int i =0 ;i < nums .length ;++i ){
15
+ int lookingFor = target -nums [i ];
16
+ if ( map .get (lookingFor )!=null ){
17
+ return new int []{ map .get (lookingFor ), i +1 };
18
+ }else {
19
+ map .put ( nums [i ], i +1 );
20
+ }
21
+ }
22
+ return null ;
23
+ }
24
+ }
25
+
26
+ // 167 Two Sum II - Input array is sorted
27
+ public class Solution {
28
+ public int [] twoSum (int [] nums , int target ) {
29
+ int [] result = new int [2 ];
30
+ int l =0 ;
31
+ int r =nums .length ;
32
+ while (l <r ){
33
+ if (nums [l ]+nums [r ]==target ){
34
+ result [0 ]=nums [l ];
35
+ result [1 ]=nums [r ];
36
+ retrun result ;
37
+ }else if (nums [l ]+nums [r ]>target ){
38
+ r --;
39
+ }else {
40
+ l ++;
41
+ }
42
+ }
43
+ retur null ;
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments