Skip to content

Commit f6cb034

Browse files
refactor 604
1 parent 8a3affc commit f6cb034

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/main/java/com/fishercoder/solutions/_604.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,40 @@
3535
3636
*/
3737
public class _604 {
38-
public static class StringIterator {
38+
public static class Solution1 {
39+
public static class StringIterator {
3940

40-
Deque<int[]> deque;
41+
Deque<int[]> deque;
4142

42-
public StringIterator(String compressedString) {
43-
deque = new ArrayDeque<>();
44-
int len = compressedString.length();
45-
int i = 0;
46-
while (i < len) {
47-
int j = i + 1;
48-
while (j < len && Character.isDigit(compressedString.charAt(j))) {
49-
j++;
43+
public StringIterator(String compressedString) {
44+
deque = new ArrayDeque<>();
45+
int len = compressedString.length();
46+
int i = 0;
47+
while (i < len) {
48+
int j = i + 1;
49+
while (j < len && Character.isDigit(compressedString.charAt(j))) {
50+
j++;
51+
}
52+
deque.addLast(new int[]{compressedString.charAt(i) - 'A', Integer.parseInt(compressedString.substring(i + 1, j))});
53+
i = j;
5054
}
51-
deque.addLast(new int[]{compressedString.charAt(i) - 'A', Integer.parseInt(compressedString.substring(i + 1, j))});
52-
i = j;
5355
}
54-
}
5556

56-
public char next() {
57-
if (deque.isEmpty()) {
58-
return ' ';
59-
}
60-
int[] top = deque.peek();
61-
top[1]--;
62-
if (top[1] == 0) {
63-
deque.pollFirst();
57+
public char next() {
58+
if (deque.isEmpty()) {
59+
return ' ';
60+
}
61+
int[] top = deque.peek();
62+
top[1]--;
63+
if (top[1] == 0) {
64+
deque.pollFirst();
65+
}
66+
return (char) ('A' + top[0]);
6467
}
65-
return (char) ('A' + top[0]);
66-
}
6768

68-
public boolean hasNext() {
69-
return !deque.isEmpty();
69+
public boolean hasNext() {
70+
return !deque.isEmpty();
71+
}
7072
}
7173
}
7274
}

src/test/java/com/fishercoder/_604Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import static org.junit.Assert.assertEquals;
77

88
public class _604Test {
9-
private static _604.StringIterator test;
9+
private static _604.Solution1.StringIterator test;
1010

1111
@Test
1212
public void test1() {
13-
test = new _604.StringIterator("L1e2t1C1o1d1e1");
13+
test = new _604.Solution1.StringIterator("L1e2t1C1o1d1e1");
1414
System.out.println(test.hasNext());
1515
System.out.println(test.next());
1616
System.out.println(test.next());
@@ -25,7 +25,7 @@ public void test1() {
2525

2626
@Test
2727
public void test2() {
28-
test = new _604.StringIterator("L10e2t1C1o1d1e11");
28+
test = new _604.Solution1.StringIterator("L10e2t1C1o1d1e11");
2929
System.out.println(test.hasNext());
3030
System.out.println(test.next());
3131
System.out.println(test.next());
@@ -58,7 +58,7 @@ public void test2() {
5858

5959
@Test
6060
public void test3() {
61-
test = new _604.StringIterator("x6");
61+
test = new _604.Solution1.StringIterator("x6");
6262
System.out.println(test.hasNext());
6363
System.out.println(test.next());
6464
System.out.println(test.next());
@@ -73,7 +73,7 @@ public void test3() {
7373

7474
@Test
7575
public void test4() {
76-
test = new _604.StringIterator("X15D18V8");
76+
test = new _604.Solution1.StringIterator("X15D18V8");
7777
System.out.println(test.hasNext());
7878
System.out.println(test.next());
7979
System.out.println(test.next());

0 commit comments

Comments
 (0)