Skip to content

Commit 660340f

Browse files
committed
decode number
1 parent 36928dd commit 660340f

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

PuzzleCoding/src/DecodeNumber.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* A message containing letters from A-Z is being encoded to numbers using the following mapping:
3+
* <p/>
4+
* 'A' -> 1
5+
* 'B' -> 2
6+
* ...
7+
* 'Z' -> 26
8+
* Given an encoded message containing digits, determine the total number of ways to decode it.
9+
* <p/>
10+
* For example,
11+
* Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
12+
* <p/>
13+
* The number of ways decoding "12" is 2.
14+
*/
15+
public class DecodeNumber {
16+
public static void main(String[] args) {
17+
String s1 = "11";
18+
String s2 = "2019";
19+
decodeNumber(s1);
20+
decodeNumber(s2);
21+
22+
}
23+
24+
public static void decodeNumber(String s) {
25+
int len = s.length();
26+
for (int i = 0; i < len; i++) {
27+
if (!((s.charAt(i) - '0') <= 9) && !(('9' - s.charAt(i)) >= 0)) {
28+
System.out.println(s + " is not number.");
29+
return;
30+
}
31+
}
32+
int[] ways = new int[len];
33+
ways[0] = (s.charAt(0) == '0') ? 0 : 1;
34+
for (int i = 1; i < len; i++) {
35+
char p = s.charAt(i - 1);
36+
char c = s.charAt(i);
37+
if (p == '1' || (p == '2' && c <= '6') || (p == '0' && c != '0' && ways[i-1]==0)) {
38+
ways[i] = ways[i - 1] *2;
39+
} else
40+
ways[i] = ways[i - 1];
41+
}
42+
System.out.println(s + ":" + ways[len - 1]);
43+
}
44+
}

PuzzleCoding/src/JumpGame.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public static void main(String[] args) {
2121
public static void jumpGame(int[] a) {
2222
if (a.length == 0) return;
2323

24-
int[] preStep = new int[a.length];
24+
int[] previousStep = new int[a.length];
2525

26-
for (int i = 0; i < preStep.length; i++)
27-
preStep[i] = -1;
26+
for (int i = 0; i < previousStep.length; i++)
27+
previousStep[i] = -1;
2828

2929
boolean reachEnd = false;
3030
int furthest = a[0];
@@ -33,11 +33,11 @@ public static void jumpGame(int[] a) {
3333
furthest = Math.max(furthest, a[i] + i);
3434
for (int j = 0; j <= furthest; j++) {
3535
if (j < a.length) {
36-
if (preStep[j] == -1)
37-
preStep[j] = i;
36+
if (previousStep[j] == -1)
37+
previousStep[j] = i;
3838
else {
39-
if (preStep[j] > i)
40-
preStep[j] = i;
39+
if (previousStep[j] > i)
40+
previousStep[j] = i;
4141
}
4242
}
4343
}
@@ -54,7 +54,7 @@ public static void jumpGame(int[] a) {
5454
int end = a.length - 1;
5555
while (end != 0) {
5656
System.out.print(a[end] +"->");
57-
end = preStep[end];
57+
end = previousStep[end];
5858
}
5959
System.out.print(a[end]);
6060
System.out.println();

PuzzleCoding/src/TreeLeafPath.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static void leafPath(TreeLeafPath node, Stack<TreeLeafPath> stack) {
6666

6767
}
6868

69+
6970
public static void main(String[] args) {
7071

7172
TreeLeafPath root = new TreeLeafPath(12);
@@ -91,5 +92,7 @@ public static void main(String[] args) {
9192

9293
Stack<TreeLeafPath> stack = new Stack<TreeLeafPath>();
9394
leafPath(root, stack);
95+
System.out.println();
96+
9497
}
9598
}

0 commit comments

Comments
 (0)