forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_604.java
43 lines (37 loc) · 1.24 KB
/
_604.java
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
package com.fishercoder.solutions;
import java.util.ArrayDeque;
import java.util.Deque;
public class _604 {
public static class Solution1 {
public static class StringIterator {
Deque<int[]> deque;
public StringIterator(String compressedString) {
deque = new ArrayDeque<>();
int len = compressedString.length();
int i = 0;
while (i < len) {
int j = i + 1;
while (j < len && Character.isDigit(compressedString.charAt(j))) {
j++;
}
deque.addLast(new int[]{compressedString.charAt(i) - 'A', Integer.parseInt(compressedString.substring(i + 1, j))});
i = j;
}
}
public char next() {
if (deque.isEmpty()) {
return ' ';
}
int[] top = deque.peek();
top[1]--;
if (top[1] == 0) {
deque.pollFirst();
}
return (char) ('A' + top[0]);
}
public boolean hasNext() {
return !deque.isEmpty();
}
}
}
}