-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathDesignCompressedStringIterator604.java
122 lines (104 loc) · 3.48 KB
/
DesignCompressedStringIterator604.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Design and implement a data structure for a compressed string iterator. It
* should support the following operations: next and hasNext.
*
* The given compressed string will be in the form of each letter followed by
* a positive integer representing the number of this letter existing in the
* original uncompressed string.
*
* next() - if the original string still has uncompressed characters, return
* the next letter; Otherwise return a white space.
* hasNext() - Judge whether there is any letter needs to be uncompressed.
*
* Note:
* Please remember to RESET your class variables declared in StringIterator,
* as static/class variables are persisted across multiple test cases.
* Please see here for more details.
*
* Example:
*
* StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1");
*
* iterator.next(); // return 'L'
* iterator.next(); // return 'e'
* iterator.next(); // return 'e'
* iterator.next(); // return 't'
* iterator.next(); // return 'C'
* iterator.next(); // return 'o'
* iterator.next(); // return 'd'
* iterator.hasNext(); // return true
* iterator.next(); // return 'e'
* iterator.hasNext(); // return false
* iterator.next(); // return ' '
*/
public class DesignCompressedStringIterator604 {
class StringIterator {
private int index;
private int count;
private int countLen;
private char[] chars;
private int len;
public StringIterator(String compressedString) {
chars = compressedString.toCharArray();
len = chars.length;
index = 0;
move();
}
public char next() {
if (index >= len) return ' ';
char returned = chars[index];
count--;
if (count == 0) {
index += countLen + 1;
move();
}
return returned;
}
public boolean hasNext() {
return index < len;
}
private void move() {
if (index < len) {
int i = index + 1;
while (i < len && isNumeric(chars[i])) {
i++;
}
countLen = i - index - 1;
count = Integer.parseInt(new String(chars, index + 1, countLen));
}
}
private boolean isNumeric(char c) {
return c >= '0' && c <= '9';
}
}
/**
* https://leetcode.com/problems/design-compressed-string-iterator/discuss/103828/Java-Concise-Single-Queue-Solution
*/
class StringIterator2 {
Queue<int[]> queue = new LinkedList<>();
public StringIterator(String s) {
int i = 0, n = s.length();
while (i < n) {
int j = i+1;
while (j < n && s.charAt(j) - 'A' < 0) j++;
queue.add(new int[]{s.charAt(i) - 'A', Integer.parseInt(s.substring(i+1, j))});
i = j;
}
}
public char next() {
if (queue.isEmpty()) return ' ';
int[] top = queue.peek();
if (--top[1] == 0) queue.poll();
return (char) ('A' + top[0]);
}
public boolean hasNext() {
return !queue.isEmpty();
}
}
/**
* Your StringIterator object will be instantiated and called as such:
* StringIterator obj = new StringIterator(compressedString);
* char param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/
}