@@ -12,7 +12,6 @@ public static void main(String[] args) {
1212 a [i ] = 0 + (int ) (Math .random () * length );
1313 }
1414
15-
1615 System .out .println (Arrays .toString (a ));
1716
1817 int [] a1 = new int []{7 , 8 , 9 , 10 , 13 , 0 , 1 , 2 , 0 , 9 , 13 , 14 };
@@ -45,12 +44,12 @@ public static void main(String[] args) {
4544 System .out .println ("a1 rank " + rank + " : " + pick );
4645
4746 int a3 = 134 ;
48- System .out .println (a3 + " reversed to " + reverseInt (a3 ));
47+ System .out .println (a3 + " reversed to " + isPalindrome1 (a3 ));
4948
5049 int a4 = 1551 ;
5150 System .out .println (a4 + " isPalindrome: " + isPalindrome (a4 ));
5251
53- int [] a5 = new int []{3 , 5 , 10 , - 1 , 1 };
52+ int [] a5 = new int []{3 , - 1 , 1 , 2 , 4 };
5453
5554 System .out .println (Arrays .toString (a5 ) + " 1st missing positiv: " + firstMissingPositive (a5 , a5 .length ));
5655
@@ -63,7 +62,7 @@ public static void main(String[] args) {
6362 removeDupInplace (a8 );
6463 System .out .println ("remove dup of a sorted arr in place: " + Arrays .toString (a8 ));
6564
66- int [] a9 = new int []{-1 , -1 , 1 , 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 , 6 , 7 ,7 , 8 };
65+ int [] a9 = new int []{-1 , -1 , 1 , 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 , 6 , 7 , 7 , 8 };
6766 removeDupInplace2 (a9 );
6867 System .out .println ("remove dup of a sorted arr in place2: " + Arrays .toString (a9 ));
6968
@@ -193,7 +192,6 @@ public static void removeDupInplace1(int[] a) {
193192 if (a [j ] != a [i ]) {
194193 a [j + 1 ] = a [i ];
195194 j ++;
196-
197195 }
198196 }
199197 j ++;
@@ -203,6 +201,7 @@ public static void removeDupInplace1(int[] a) {
203201 }
204202 }
205203
204+ // not a good solution
206205 public static void removeDupInplace (int [] a ) {
207206 if (a .length <= 1 ) return ;
208207
@@ -230,8 +229,6 @@ public static void removeDupInplace(int[] a) {
230229 }
231230 if (a [i ] == 0 )
232231 return ;
233-
234-
235232 }
236233
237234 }
@@ -252,14 +249,9 @@ public static void mergeSorted(int[] l, int[] s) {
252249 while (k >= 0 ) {
253250 if (s [k ] >= l [j ]) {
254251 l [i --] = s [k --];
255- //i--;
256- //k--;
257252 } else {
258253 l [i --] = l [j --];
259- //i--;
260- //j--;
261254 }
262-
263255 }
264256
265257 while (k >= 0 ) {
@@ -268,16 +260,17 @@ public static void mergeSorted(int[] l, int[] s) {
268260 }
269261
270262 public static int firstMissingPositive (int A [], int n ) {
263+
264+ if (A .length == 0 ) return 1 ;
265+
271266 //The key idea is to put every element in A such that A[i]=i+1
272267 for (int i = 0 ; i < n ; i ++) {
273- //System.out.println(Arrays.toString(A));
274268 while (A [i ] != i + 1 && A [i ] > 0 && A [i ] <= n && A [i ] != A [A [i ] - 1 ]) {
275269 int temp = A [i ];
276270 A [i ] = A [temp - 1 ];
277271 A [temp - 1 ] = temp ;
278272 }
279273 }
280- //System.out.println(Arrays.toString(A));
281274 for (int i = 0 ; i < n ; i ++) {
282275 if (A [i ] != i + 1 ) return i + 1 ;
283276 }
@@ -300,6 +293,11 @@ public static boolean isPalindrome(int x) {
300293 return true ;
301294 }
302295
296+ public static boolean isPalindrome1 (int x ) {
297+ if (x < 0 ) return false ;
298+ return x == reverseInt (x );
299+ }
300+
303301 public static int reverseInt (int a ) {
304302 int r = 0 ;
305303 while (a != 0 ) {
@@ -313,54 +311,47 @@ public static int reverseInt(int a) {
313311 // longest increasing sequence
314312 public static ArrayList <Integer > LIS (int [] a ) {
315313
316- LinkedList <Integer > memo = new LinkedList <Integer >();
314+ LinkedList <Integer > trackLargestIndex = new LinkedList <Integer >();
317315 int [] path = new int [a .length ];
318316
319- memo .add (0 );
317+ trackLargestIndex .add (0 );
320318 path [0 ] = 0 ;
321319
322320 for (int i = 1 ; i < a .length ; ++i ) {
323321
324- if (a [memo .getLast ()] <= a [i ]) {
325- path [i ] = memo .getLast (); // record the index of previous smaller element
326- memo .add (i ); // record the index of the largest element
322+ if (a [trackLargestIndex .getLast ()] <= a [i ]) {
323+ path [i ] = trackLargestIndex .getLast (); // record the index of previous smaller element
324+ trackLargestIndex .add (i ); // record the index of the largest element
327325 } else {
328326 int s , e ;
329- for (s = 0 , e = memo .size () - 1 ; s < e ; ) {
327+ for (s = 0 , e = trackLargestIndex .size () - 1 ; s < e ; ) {
330328
331329 int m = (e + s ) / 2 ;
332330
333- if (a [memo .get (m )] <= a [i ])
331+ if (a [trackLargestIndex .get (m )] <= a [i ])
334332 s = m + 1 ;
335333 else
336334 e = m ;
337335
338336 }
339- if (a [i ] < a [memo .get (s )]) {
340- memo .set (s , i );
337+ if (a [i ] < a [trackLargestIndex .get (s )]) {
338+ trackLargestIndex .set (s , i );
341339 if (s > 0 )
342- path [i ] = memo .get (s - 1 );
340+ path [i ] = trackLargestIndex .get (s - 1 );
343341 else
344342 path [i ] = 0 ;
345343 }
346-
347344 }
348-
349345 }
350346
351- int pos = memo .getLast ();
347+ int index = trackLargestIndex .getLast ();
352348 ArrayList <Integer > lis = new ArrayList <Integer >();
353- for (int n = 0 ; n < memo .size (); ++n ) {
354- lis .add (0 , a [pos ]);
355- pos = path [pos ];
349+ for (int n = 0 ; n < trackLargestIndex .size (); ++n ) {
350+ lis .add (0 , a [index ]);
351+ index = path [index ];
356352 }
357353
358- // System.out.println("LIS: " + lis.toString());
359- // reverse the order
360- Collections .sort (lis );
361-
362354 return lis ;
363-
364355 }
365356
366357 public static int LIS1 (int [] a ) {
@@ -390,9 +381,7 @@ else if (a[i] >= a[trackTable[max]]) {
390381 }
391382
392383 }
393- //System.out.println(Arrays.toString(trackTable));
394384 return max ;
395-
396385 }
397386
398387 public static int partition (int [] a , int left , int right ) {
@@ -402,17 +391,17 @@ public static int partition(int[] a, int left, int right) {
402391 right --;
403392 while (a [left ] < pivot )
404393 left ++;
394+
405395 if (left <= right ) {
406396 int tmp = a [right ];
407397 a [right ] = a [left ];
408398 a [left ] = tmp ;
399+
409400 right --;
410401 left ++;
411402 }
412403
413404 }
414- // System.out.print(pivot + " : " );
415- // System.out.println(Arrays.toString(a));
416405 return left ;
417406 }
418407
@@ -439,7 +428,6 @@ public static int pickRank(int[] a, int left, int right, int rank) {
439428 if (leftSize == rank )
440429 return max (a , left , pivotIndex );
441430 else if (rank < leftSize ) {
442- // System.out.println(left +" "+ pivotIndex + " " + rank );
443431 return pickRank (a , left , pivotIndex - 1 , rank );
444432 } else //(rank > leftSize)
445433 return pickRank (a , pivotIndex , right , rank - leftSize );
@@ -456,7 +444,6 @@ public static void mergeSort(int[] a, int s, int e) {
456444
457445 public static void merge (int [] a , int left , int middle , int right ) {
458446
459- // int[] helper = a.clone();
460447 int [] helper = new int [a .length ];
461448
462449 for (int i = left ; i <= right ; ++i ) {
@@ -475,9 +462,7 @@ public static void merge(int[] a, int left, int middle, int right) {
475462 a [current ] = helper [helperMiddle ];
476463 helperMiddle --;
477464 current --;
478-
479465 }
480-
481466 }
482467
483468 while (helperRight > middle ) {
@@ -488,5 +473,4 @@ public static void merge(int[] a, int left, int middle, int right) {
488473
489474 }
490475
491-
492476}
0 commit comments