@@ -12,7 +12,6 @@ public static void main(String[] args) {
12
12
a [i ] = 0 + (int ) (Math .random () * length );
13
13
}
14
14
15
-
16
15
System .out .println (Arrays .toString (a ));
17
16
18
17
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) {
45
44
System .out .println ("a1 rank " + rank + " : " + pick );
46
45
47
46
int a3 = 134 ;
48
- System .out .println (a3 + " reversed to " + reverseInt (a3 ));
47
+ System .out .println (a3 + " reversed to " + isPalindrome1 (a3 ));
49
48
50
49
int a4 = 1551 ;
51
50
System .out .println (a4 + " isPalindrome: " + isPalindrome (a4 ));
52
51
53
- int [] a5 = new int []{3 , 5 , 10 , - 1 , 1 };
52
+ int [] a5 = new int []{3 , - 1 , 1 , 2 , 4 };
54
53
55
54
System .out .println (Arrays .toString (a5 ) + " 1st missing positiv: " + firstMissingPositive (a5 , a5 .length ));
56
55
@@ -63,7 +62,7 @@ public static void main(String[] args) {
63
62
removeDupInplace (a8 );
64
63
System .out .println ("remove dup of a sorted arr in place: " + Arrays .toString (a8 ));
65
64
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 };
67
66
removeDupInplace2 (a9 );
68
67
System .out .println ("remove dup of a sorted arr in place2: " + Arrays .toString (a9 ));
69
68
@@ -193,7 +192,6 @@ public static void removeDupInplace1(int[] a) {
193
192
if (a [j ] != a [i ]) {
194
193
a [j + 1 ] = a [i ];
195
194
j ++;
196
-
197
195
}
198
196
}
199
197
j ++;
@@ -203,6 +201,7 @@ public static void removeDupInplace1(int[] a) {
203
201
}
204
202
}
205
203
204
+ // not a good solution
206
205
public static void removeDupInplace (int [] a ) {
207
206
if (a .length <= 1 ) return ;
208
207
@@ -230,8 +229,6 @@ public static void removeDupInplace(int[] a) {
230
229
}
231
230
if (a [i ] == 0 )
232
231
return ;
233
-
234
-
235
232
}
236
233
237
234
}
@@ -252,14 +249,9 @@ public static void mergeSorted(int[] l, int[] s) {
252
249
while (k >= 0 ) {
253
250
if (s [k ] >= l [j ]) {
254
251
l [i --] = s [k --];
255
- //i--;
256
- //k--;
257
252
} else {
258
253
l [i --] = l [j --];
259
- //i--;
260
- //j--;
261
254
}
262
-
263
255
}
264
256
265
257
while (k >= 0 ) {
@@ -268,16 +260,17 @@ public static void mergeSorted(int[] l, int[] s) {
268
260
}
269
261
270
262
public static int firstMissingPositive (int A [], int n ) {
263
+
264
+ if (A .length == 0 ) return 1 ;
265
+
271
266
//The key idea is to put every element in A such that A[i]=i+1
272
267
for (int i = 0 ; i < n ; i ++) {
273
- //System.out.println(Arrays.toString(A));
274
268
while (A [i ] != i + 1 && A [i ] > 0 && A [i ] <= n && A [i ] != A [A [i ] - 1 ]) {
275
269
int temp = A [i ];
276
270
A [i ] = A [temp - 1 ];
277
271
A [temp - 1 ] = temp ;
278
272
}
279
273
}
280
- //System.out.println(Arrays.toString(A));
281
274
for (int i = 0 ; i < n ; i ++) {
282
275
if (A [i ] != i + 1 ) return i + 1 ;
283
276
}
@@ -300,6 +293,11 @@ public static boolean isPalindrome(int x) {
300
293
return true ;
301
294
}
302
295
296
+ public static boolean isPalindrome1 (int x ) {
297
+ if (x < 0 ) return false ;
298
+ return x == reverseInt (x );
299
+ }
300
+
303
301
public static int reverseInt (int a ) {
304
302
int r = 0 ;
305
303
while (a != 0 ) {
@@ -313,54 +311,47 @@ public static int reverseInt(int a) {
313
311
// longest increasing sequence
314
312
public static ArrayList <Integer > LIS (int [] a ) {
315
313
316
- LinkedList <Integer > memo = new LinkedList <Integer >();
314
+ LinkedList <Integer > trackLargestIndex = new LinkedList <Integer >();
317
315
int [] path = new int [a .length ];
318
316
319
- memo .add (0 );
317
+ trackLargestIndex .add (0 );
320
318
path [0 ] = 0 ;
321
319
322
320
for (int i = 1 ; i < a .length ; ++i ) {
323
321
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
327
325
} else {
328
326
int s , e ;
329
- for (s = 0 , e = memo .size () - 1 ; s < e ; ) {
327
+ for (s = 0 , e = trackLargestIndex .size () - 1 ; s < e ; ) {
330
328
331
329
int m = (e + s ) / 2 ;
332
330
333
- if (a [memo .get (m )] <= a [i ])
331
+ if (a [trackLargestIndex .get (m )] <= a [i ])
334
332
s = m + 1 ;
335
333
else
336
334
e = m ;
337
335
338
336
}
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 );
341
339
if (s > 0 )
342
- path [i ] = memo .get (s - 1 );
340
+ path [i ] = trackLargestIndex .get (s - 1 );
343
341
else
344
342
path [i ] = 0 ;
345
343
}
346
-
347
344
}
348
-
349
345
}
350
346
351
- int pos = memo .getLast ();
347
+ int index = trackLargestIndex .getLast ();
352
348
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 ];
356
352
}
357
353
358
- // System.out.println("LIS: " + lis.toString());
359
- // reverse the order
360
- Collections .sort (lis );
361
-
362
354
return lis ;
363
-
364
355
}
365
356
366
357
public static int LIS1 (int [] a ) {
@@ -390,9 +381,7 @@ else if (a[i] >= a[trackTable[max]]) {
390
381
}
391
382
392
383
}
393
- //System.out.println(Arrays.toString(trackTable));
394
384
return max ;
395
-
396
385
}
397
386
398
387
public static int partition (int [] a , int left , int right ) {
@@ -402,17 +391,17 @@ public static int partition(int[] a, int left, int right) {
402
391
right --;
403
392
while (a [left ] < pivot )
404
393
left ++;
394
+
405
395
if (left <= right ) {
406
396
int tmp = a [right ];
407
397
a [right ] = a [left ];
408
398
a [left ] = tmp ;
399
+
409
400
right --;
410
401
left ++;
411
402
}
412
403
413
404
}
414
- // System.out.print(pivot + " : " );
415
- // System.out.println(Arrays.toString(a));
416
405
return left ;
417
406
}
418
407
@@ -439,7 +428,6 @@ public static int pickRank(int[] a, int left, int right, int rank) {
439
428
if (leftSize == rank )
440
429
return max (a , left , pivotIndex );
441
430
else if (rank < leftSize ) {
442
- // System.out.println(left +" "+ pivotIndex + " " + rank );
443
431
return pickRank (a , left , pivotIndex - 1 , rank );
444
432
} else //(rank > leftSize)
445
433
return pickRank (a , pivotIndex , right , rank - leftSize );
@@ -456,7 +444,6 @@ public static void mergeSort(int[] a, int s, int e) {
456
444
457
445
public static void merge (int [] a , int left , int middle , int right ) {
458
446
459
- // int[] helper = a.clone();
460
447
int [] helper = new int [a .length ];
461
448
462
449
for (int i = left ; i <= right ; ++i ) {
@@ -475,9 +462,7 @@ public static void merge(int[] a, int left, int middle, int right) {
475
462
a [current ] = helper [helperMiddle ];
476
463
helperMiddle --;
477
464
current --;
478
-
479
465
}
480
-
481
466
}
482
467
483
468
while (helperRight > middle ) {
@@ -488,5 +473,4 @@ public static void merge(int[] a, int left, int middle, int right) {
488
473
489
474
}
490
475
491
-
492
476
}
0 commit comments