Skip to content

Commit 097bd0b

Browse files
committed
Updated astroid
1 parent 900fbef commit 097bd0b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Other/Astroid.java renamed to Other/Astroid01.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
*
77
* Tags: Backtracking
88
*/
9-
class Astroid {
9+
class Astroid01 {
10+
1011
public static void main(String[] args) {
11-
Astroid a = new Astroid();
12+
Astroid01 a = new Astroid01();
1213
System.out.println(a.astroid("01*0*"));
1314
}
1415

@@ -20,22 +21,22 @@ public List<String> astroid(String s) {
2021
}
2122

2223
/**
23-
* Backtracking on the string
24+
* Backtracking, generate all possible result
2425
* Replace if current char is *
2526
* Otherwise, go to next position
2627
*/
2728
public void astroid(String s, int pos, StringBuilder sb, List<String> res) {
2829
if (pos == sb.length()) {
29-
res.add(sb.toString());
30+
res.add(sb.toString()); // found a result
3031
return;
3132
}
3233
if (s.charAt(pos) == '*') {
33-
sb.setCharAt(pos, '0');
34-
astroid(s, pos + 1, sb, res);
35-
sb.setCharAt(pos, '1');
36-
astroid(s, pos + 1, sb, res);
34+
sb.setCharAt(pos, '0'); // replace with 0
35+
astroid(s, pos + 1, sb, res); // recurse
36+
sb.setCharAt(pos, '1'); // replace with 1
37+
astroid(s, pos + 1, sb, res); // recurse
3738
} else {
38-
astroid(s, pos + 1, sb, res);
39+
astroid(s, pos + 1, sb, res);
3940
}
4041
}
4142
}

0 commit comments

Comments
 (0)