-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_604.java
More file actions
53 lines (44 loc) · 1.34 KB
/
Copy pathP_604.java
File metadata and controls
53 lines (44 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package leetcode.easy;
import java.util.ArrayDeque;
import java.util.Deque;
public class P_604 {
static class Pair {
char c;
int count;
Pair(char c, int count) {
this.c = c;
this.count = count;
}
}
static class StringIterator {
Deque<Pair> stack = new ArrayDeque<>();
StringIterator(String compressedString) {
int i = 0;
while (i < compressedString.length()) {
final char c = compressedString.charAt(i);
i++;
int countEnd = i;
while (countEnd < compressedString.length()
&& Character.isDigit(compressedString.charAt(countEnd))) {
countEnd++;
}
stack.addLast(new Pair(c, Integer.parseInt(compressedString.substring(i, countEnd))));
i = countEnd;
}
}
public char next() {
if (!stack.isEmpty()) {
if (stack.peekFirst().count == 1) {
return stack.removeFirst().c;
}
stack.peekFirst().count--;
return stack.peekFirst().c;
} else {
return ' ';
}
}
public boolean hasNext() {
return !stack.isEmpty();
}
}
}