Skip to content

Commit bec127f

Browse files
committed
add comments/fix bugs
1 parent 171b5ef commit bec127f

31 files changed

+152
-133
lines changed

IntArrayOps/src/IntArrayOps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public static ArrayList<Integer> LIS(int[] a) {
323323

324324
if (a[memo.getLast()] <= a[i]) {
325325
path[i] = memo.getLast(); // record the index of previous smaller element
326-
memo.add(i); // record the largest element
326+
memo.add(i); // record the index of the largest element
327327
} else {
328328
int s, e;
329329
for (s = 0, e = memo.size() - 1; s < e; ) {

PuzzleCoding/src/AddBinary.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@
77
*/
88
public class AddBinary {
99
public static void main(String[] args) {
10-
String s1 = "11";
10+
String s1 = "10";
1111
String s2 = "110";
1212

1313
int result = string2Integer(s1, 2) + string2Integer(s2, 2);
1414
System.out.println(toBinary(result));
1515
result = Integer.parseInt(s1, 2) + Integer.parseInt(s2, 2);
1616
System.out.println(Integer.toBinaryString(result));
1717

18-
System.out.print(~(0 & -1));
19-
20-
2118
}
2219

2320
public static int string2Integer(String s, int code) {
2421
int len = s.length();
2522
int result = 0;
26-
for (int i = 0; i < len; ++i) {
27-
int temp = Integer.valueOf(s.substring(len - i - 1, len - i));
28-
result += Math.pow(code, i) * temp;
23+
for (int i = len -1 ; i >=0 ; i--) {
24+
int temp = Integer.valueOf(s.charAt(i)-'0');
25+
result += Math.pow(code, len-1-i) * temp;
2926
}
3027
return result;
3128
}

PuzzleCoding/src/AddTwoNumber.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*/
1010
public class AddTwoNumber {
1111
public static void main(String[] args) {
12-
int[] a = {2, 4, 3};
13-
int[] b = {6, 4};
12+
int[] a = {2, 9, 3};
13+
int[] b = {9, 9};
1414
ArrayList<Integer> result = addTwoNumber(a, b);
1515
System.out.println(result);
1616
}
@@ -25,7 +25,7 @@ public static ArrayList<Integer> addTwoNumber(int[] n1, int[] n2) {
2525
while (l1 >= 0 && l2 >= 0) {
2626
int tmp = n1[l1--] + n2[l2--] + carry;
2727
result.add(0, tmp % 10);
28-
if ((tmp + carry) / 10 >= 1)
28+
if (tmp / 10 >= 1)
2929
carry = 1;
3030
else
3131
carry = 0;
@@ -37,7 +37,7 @@ public static ArrayList<Integer> addTwoNumber(int[] n1, int[] n2) {
3737
while (len >= 0) {
3838
int tmp = n[len--] + carry;
3939
result.add(0, tmp % 10);
40-
if ((tmp + carry) / 10 >= 1)
40+
if (tmp / 10 >= 1)
4141
carry = 1;
4242
else
4343
carry = 0;

PuzzleCoding/src/BSTtoDLL.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public static void main(String[] args) {
2323
Node node12 = new Node(12);
2424
Node node14 = new Node(14);
2525

26-
2726
root.insert(node5);
2827
root.insert(node6);
2928
root.insert(node15);
@@ -36,7 +35,6 @@ public static void main(String[] args) {
3635
printDll(dll);
3736

3837
bstToDllinPlace(root);
39-
4038
printDll(head);
4139

4240
}
@@ -91,10 +89,8 @@ public static ArrayList<Node> bstToDLL(Node node) {
9189
return dll;
9290
}
9391

94-
95-
static Node prev, head;
96-
9792
// in place approach
93+
static Node prev, head;
9894
public static void bstToDllinPlace(Node p) {
9995
if (p == null)
10096
return;
@@ -106,9 +102,9 @@ public static void bstToDllinPlace(Node p) {
106102
if (prev == null) {
107103
head = p;
108104
} else {
109-
110105
prev.right = p;
111106
}
107+
112108
System.out.print("parent: " + p.value + " prev: ");
113109
nodeToString(prev);
114110
System.out.print(" head: ");
@@ -135,7 +131,6 @@ public static void bstToDllinPlace(Node p) {
135131
public static void nodeToString(Node n) {
136132
if (n == null) {
137133
System.out.println("null");
138-
139134
} else {
140135
System.out.println(n.value);
141136
}

PuzzleCoding/src/BTreeIterator.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1+
import java.util.LinkedList;
12
import java.util.NoSuchElementException;
3+
import java.util.Queue;
24
import java.util.Stack;
35

46
public class BTreeIterator {
57
private Node root;
68
private Stack<Node> stack;
79
private Stack<Node> preOrderStack;
8-
private Stack<Node> leftVisitedStack;
10+
private Queue<Node> postOrderQueue;
911

1012
public BTreeIterator(Node n) {
1113
root = n;
1214
stack = new Stack<Node>();
13-
leftVisitedStack = new Stack<Node>();
15+
pushLeft(root);
16+
17+
// for post-order, no need for in-order
18+
postOrderQueue = new LinkedList<Node>();
19+
pushPostOrder(root);
20+
// for pre-order
1421
preOrderStack = new Stack<Node>();
1522
preOrderStack.push(root);
16-
pushLeft(root);
1723
}
18-
24+
//in-order
1925
public boolean hasNext() {
2026
return !stack.isEmpty();
2127
}
2228

2329
public void reset() {
2430
stack.empty();
25-
leftVisitedStack.empty();
31+
postOrderQueue.clear();
2632
pushLeft(root);
2733
}
2834

@@ -34,7 +40,7 @@ public Node next() {
3440
return inOrderNext();
3541
}
3642

37-
// in-order
43+
// in-order : left, root, right
3844
public Node inOrderNext() {
3945
if (hasNext()) {
4046
Node node = stack.pop();
@@ -52,11 +58,11 @@ private void pushLeft(Node node) {
5258
}
5359
}
5460

55-
5661
public boolean hasNextPreOrder(){
5762
return !preOrderStack.isEmpty();
5863
}
5964

65+
//pre-order: root, left, right
6066
public Node preOrderNext() {
6167
if (!preOrderStack.isEmpty()) {
6268
Node node = preOrderStack.pop();
@@ -71,21 +77,23 @@ public Node preOrderNext() {
7177
return null;
7278
}
7379
}
74-
80+
public void pushPostOrder(Node n){
81+
if(n == null)
82+
return;
83+
if(n.left != null)
84+
pushPostOrder(n.left);
85+
if(n.right != null)
86+
pushPostOrder(n.right);
87+
88+
postOrderQueue.add(n);
89+
}
90+
public boolean hasNextPostOrder(){
91+
return !postOrderQueue.isEmpty();
92+
}
93+
//post-order: left, right, root
7594
public Node postOrderNext() {
76-
if (hasNext()) {
77-
if (stack.peek().right == null ||
78-
leftVisitedStack.contains(stack.peek().right)) {
79-
80-
Node node = stack.pop();
81-
return node;
82-
83-
} else {
84-
leftVisitedStack.push(stack.peek().right);
85-
pushLeft(stack.peek().right);
86-
return postOrderNext();
87-
}
88-
95+
if (!postOrderQueue.isEmpty()) {
96+
return postOrderQueue.poll();
8997
} else {
9098
throw new NoSuchElementException();
9199
}

PuzzleCoding/src/BuildBT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static Node buildBTInorderPreorder(int[] in,
4040
if (in.length == 0 || pre.length != in.length)
4141
return null;
4242

43+
// get index of element of inOrder
4344
HashMap<Integer, Integer> index = new HashMap<Integer, Integer>();
4445
for (int i = 0; i < in.length; i++) {
4546
index.put(in[i], i);

PuzzleCoding/src/ClimbStairs.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static void main(String[] args) {
1717
public static int climbStairs(int n) {
1818
if (0 < n && n <= 1) return n;
1919
else if (n <= 0) return 1;
20-
else return climbStairs(n - 1) + climbStairs(n - 2) + climbStairs(n - 3); // add the last step 1 or 2 or 3.
20+
else return climbStairs(n - 1)
21+
+ climbStairs(n - 2)
22+
+ climbStairs(n - 3); // add the last step 1 or 2 or 3.
2123
}
2224
}

PuzzleCoding/src/CombinationPermutation.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ public class CombinationPermutation {
44
public static void main(String[] args) {
55

66
String s = "abcd";
7-
int k = 2;
7+
int k = 3;
88
System.out.println("Combination");
99
combinationK("", s, k);
1010
System.out.println("Permutation");
1111
permutationK(s.toCharArray(), s.length(), k);
12+
System.out.println("Permutation1");
13+
permutationK1("", s, k);
1214
System.out.println("MoveInOut");
1315
move(s.toCharArray(), s.length(), new ArrayList<String>(), true);
1416

@@ -23,6 +25,15 @@ public static void combinationK(String prefix, String s, int K) {
2325
combinationK(prefix + s.charAt(i), s.substring(i + 1), K - 1);
2426
}
2527

28+
public static void permutationK1(String prefix, String s, int K) {
29+
if (K == 0) {
30+
System.out.println(prefix);
31+
return;
32+
}
33+
for (int i = 0; i < s.length(); ++i)
34+
permutationK1(prefix + s.charAt(i), s.substring(0,i)+s.substring(i + 1), K - 1);
35+
}
36+
2637
public static void permutationK(char[] s, int n, int K) {
2738
if (K == 0) {
2839
for (int i = n; i < s.length; ++i)

PuzzleCoding/src/CombinationSum.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import java.util.ArrayList;
22

3-
43
/*
54
Given a set of candidate numbers (C) and a target number (T),
65
find all unique combinations in C where the candidate numbers sums to T.
@@ -19,7 +18,7 @@ All numbers (including target) will be positive integers.
1918
*/
2019
public class CombinationSum {
2120
public static void main(String[] args) {
22-
int[] a = new int[]{2, 3, 6, 7, 10};
21+
int[] a = new int[]{2, 3, 5, 6, 7, 10};
2322
int target = 10;
2423

2524
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
@@ -36,7 +35,7 @@ public static void combinationSum(int[] a,
3635
ArrayList<Integer> r,
3736
ArrayList<ArrayList<Integer>> results) {
3837

39-
38+
// find it: target == 0
4039
if (target == 0) {
4140
results.add(new ArrayList<Integer>(r));
4241
return;

PuzzleCoding/src/CompareArrayBST.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* <p/>
77
* e.g [3 2 1 0 5 4 6] & [3 5 2 6 4 1 0]
88
* when the BST is formed by taking the elements from the left,
9-
* both BST turn out to be same.
9+
* both BST turn out to be same. (in-order)
1010
* <p/>
1111
* 3
1212
* / \
@@ -50,6 +50,7 @@ public static boolean identicalBST(int[] a, int[] b) {
5050
ArrayList<Integer> bSmaller = new ArrayList<Integer>();
5151
ArrayList<Integer> bLarger = new ArrayList<Integer>();
5252

53+
//a[0] and b[0] are the roots of trees.
5354
for (int i = 1; i < a.length; i++) {
5455
if (a[i] < a[0])
5556
aSmaller.add(a[i]);

PuzzleCoding/src/DecodeNumber.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
public class DecodeNumber {
1616
public static void main(String[] args) {
1717
String s1 = "11";
18-
String s2 = "2019";
18+
String s2 = "2119";
19+
String s3 = "116";
1920
decodeNumber(s1);
2021
decodeNumber(s2);
21-
22+
decodeNumber(s3);
2223
}
2324

2425
public static void decodeNumber(String s) {
@@ -41,13 +42,17 @@ public static void decodeNumber(String s) {
4142
for (int i = 1; i < len; i++) {
4243
char p = s.charAt(i - 1);
4344
char c = s.charAt(i);
44-
if (p == '0' && c != '0' && ways[i - 1] == 0) {
45-
ways[i - 1] = 1;
45+
if (p !='0' && ways[i - 1] == 0)
46+
ways[i] = 1;
47+
if ( ways[i-1] != 0) {
48+
ways[i] = ways[i-1];
4649
}
4750
if (p == '1' || (p == '2' && c <= '6')) {
48-
ways[i] = ways[i - 1] * 2;
49-
} else
50-
ways[i] = ways[i - 1];
51+
if(i>=2)
52+
ways[i] = ways[i - 2] + ways[i];
53+
else
54+
ways[i] = ways[i-1] + ways[i];
55+
}
5156
}
5257
System.out.println(s + ":" + ways[len - 1]);
5358
}

PuzzleCoding/src/FindMiddleShiftedSorted.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class FindMiddleShiftedSorted {
44
public static void main(String[] args) {
55
int[] a1 = new int[]{15, 17, 1, 3, 6, 7};
66
int[] a2 = new int[]{15, 16, 17, 18, 1};
7-
int[] a3 = new int[]{1, 2, 3, 4};
7+
int[] a3 = new int[]{1, 2, 3, 4, 5};
88
findMiddleShiftedSorted(a1);
99
findMiddleShiftedSorted(a2);
1010
findMiddleShiftedSorted(a3);

PuzzleCoding/src/GenerateParentheses.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ public static void generateParentheses(char[] prefix, int left, int right, int l
1313
System.out.println(String.valueOf(prefix));
1414
return;
1515
} else {
16-
if (left > 0 && right >= 0) {
16+
if (left > 0 ) {
1717
prefix[level] = '(';
1818
generateParentheses(prefix, left - 1, right, level + 1);
1919
}
20-
if (right > 0 && left >= 0) {
20+
if (right > 0 ) {
2121
prefix[level] = ')';
2222
generateParentheses(prefix, left, right - 1, level + 1);
2323
}
2424
}
25-
2625
}
2726

28-
2927
public static boolean isGoodParentheses(char[] s) {
3028

3129
int level1 = 0;

PuzzleCoding/src/GrayCode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ public static String[] grayCode(int n) {
8484
public static String toBinaryString(int integer) {
8585
StringBuilder builder = new StringBuilder();
8686
int temp;
87-
while (integer >= 0) { // don't 0 case
87+
while (integer >= 0) { // 0 case
8888
temp = integer;
8989
integer = (temp >> 1);
9090
builder.append(String.valueOf(temp % 2));
91+
// if insert at 0, no need to reverse
92+
// builder.insert(0, String.valueOf(temp % 2));
9193
if(integer ==0)
9294
break;
9395
}

PuzzleCoding/src/HanoiTower.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void moveDisks(int n, HanoiTower dest, HanoiTower aux) {
5454
}
5555

5656
public static void main(String[] args) {
57-
int N = 4;
57+
int N = 3;
5858
HanoiTower[] towers = new HanoiTower[N];
5959
for (int i = 1; i <= N; i++) {
6060
towers[i - 1] = new HanoiTower(i);

0 commit comments

Comments
 (0)