Skip to content

Commit 2075416

Browse files
committed
feat: add solutions to lc problem: No.0604
No.0604.Design Compressed String Iterator
1 parent 04cf7a6 commit 2075416

File tree

6 files changed

+532
-1
lines changed

6 files changed

+532
-1
lines changed

solution/0600-0699/0604.Design Compressed String Iterator/README.md

+185
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,207 @@ stringIterator.hasNext(); // 返回 True</pre>
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:解析存储**
57+
58+
`compressedString` 解析成字符 $c$ 和对应的重复次数 $x$,存储在数组或列表 $d$ 中,用 $p$ 指向当前字符。
59+
60+
然后在 `next``hasNext` 中进行操作。
61+
62+
初始化的时间复杂度为 $O(n)$,其余操作的时间复杂度为 $O(1)$。其中 $n$ 为 `compressedString` 的长度。
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
<!-- 这里可写当前语言的特殊实现逻辑 -->
6169

6270
```python
71+
class StringIterator:
72+
73+
def __init__(self, compressedString: str):
74+
self.d = []
75+
self.p = 0
76+
n = len(compressedString)
77+
i = 0
78+
while i < n:
79+
c = compressedString[i]
80+
x = 0
81+
i += 1
82+
while i < n and compressedString[i].isdigit():
83+
x = x * 10 + int(compressedString[i])
84+
i += 1
85+
self.d.append([c, x])
86+
87+
def next(self) -> str:
88+
if not self.hasNext():
89+
return ' '
90+
ans = self.d[self.p][0]
91+
self.d[self.p][1] -= 1
92+
if self.d[self.p][1] == 0:
93+
self.p += 1
94+
return ans
95+
96+
def hasNext(self) -> bool:
97+
return self.p < len(self.d) and self.d[self.p][1] > 0
6398

99+
100+
# Your StringIterator object will be instantiated and called as such:
101+
# obj = StringIterator(compressedString)
102+
# param_1 = obj.next()
103+
# param_2 = obj.hasNext()
64104
```
65105

66106
### **Java**
67107

68108
<!-- 这里可写当前语言的特殊实现逻辑 -->
69109

70110
```java
111+
class StringIterator {
112+
private List<Node> d = new ArrayList<>();
113+
private int p;
114+
115+
public StringIterator(String compressedString) {
116+
int n = compressedString.length();
117+
int i = 0;
118+
while (i < n) {
119+
char c = compressedString.charAt(i);
120+
int x = 0;
121+
while (++i < n && Character.isDigit(compressedString.charAt(i))) {
122+
x = x * 10 + (compressedString.charAt(i) - '0');
123+
}
124+
d.add(new Node(c, x));
125+
}
126+
}
127+
128+
public char next() {
129+
if (!hasNext()) {
130+
return ' ';
131+
}
132+
char ans = d.get(p).c;
133+
if (--d.get(p).x == 0) {
134+
++p;
135+
}
136+
return ans;
137+
}
138+
139+
public boolean hasNext() {
140+
return p < d.size() && d.get(p).x > 0;
141+
}
142+
}
143+
144+
class Node {
145+
char c;
146+
int x;
147+
148+
Node(char c, int x) {
149+
this.c = c;
150+
this.x = x;
151+
}
152+
}
153+
154+
/**
155+
* Your StringIterator object will be instantiated and called as such:
156+
* StringIterator obj = new StringIterator(compressedString);
157+
* char param_1 = obj.next();
158+
* boolean param_2 = obj.hasNext();
159+
*/
160+
```
161+
162+
### **C++**
163+
164+
```cpp
165+
class StringIterator {
166+
public:
167+
StringIterator(string compressedString) {
168+
int n = compressedString.size();
169+
int i = 0;
170+
while (i < n) {
171+
char c = compressedString[i];
172+
int x = 0;
173+
while (++i < n && isdigit(compressedString[i])) {
174+
x = x * 10 + (compressedString[i] - '0');
175+
}
176+
d.push_back({c, x});
177+
}
178+
}
179+
180+
char next() {
181+
if (!hasNext()) return ' ';
182+
char ans = d[p].first;
183+
if (--d[p].second == 0) {
184+
++p;
185+
}
186+
return ans;
187+
}
188+
189+
bool hasNext() {
190+
return p < d.size() && d[p].second > 0;
191+
}
192+
private:
193+
vector<pair<char, int>> d;
194+
int p = 0;
195+
};
196+
197+
/**
198+
* Your StringIterator object will be instantiated and called as such:
199+
* StringIterator* obj = new StringIterator(compressedString);
200+
* char param_1 = obj->next();
201+
* bool param_2 = obj->hasNext();
202+
*/
203+
```
204+
205+
### **Go**
206+
207+
```go
208+
type pair struct {
209+
c byte
210+
x int
211+
}
212+
213+
type StringIterator struct {
214+
d []pair
215+
p int
216+
}
217+
218+
func Constructor(compressedString string) StringIterator {
219+
n := len(compressedString)
220+
i := 0
221+
d := []pair{}
222+
for i < n {
223+
c := compressedString[i]
224+
x := 0
225+
i++
226+
for i < n && compressedString[i] >= '0' && compressedString[i] <= '9' {
227+
x = x*10 + int(compressedString[i]-'0')
228+
i++
229+
}
230+
d = append(d, pair{c, x})
231+
}
232+
return StringIterator{d, 0}
233+
}
234+
235+
func (this *StringIterator) Next() byte {
236+
if !this.HasNext() {
237+
return ' '
238+
}
239+
ans := this.d[this.p].c
240+
this.d[this.p].x--
241+
if this.d[this.p].x == 0 {
242+
this.p++
243+
}
244+
return ans
245+
}
246+
247+
func (this *StringIterator) HasNext() bool {
248+
return this.p < len(this.d) && this.d[this.p].x > 0
249+
}
71250

251+
/**
252+
* Your StringIterator object will be instantiated and called as such:
253+
* obj := Constructor(compressedString);
254+
* param_1 := obj.Next();
255+
* param_2 := obj.HasNext();
256+
*/
72257
```
73258

74259
### **...**

solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md

+178-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,190 @@ stringIterator.hasNext(); // return True
5353
### **Python3**
5454

5555
```python
56-
56+
class StringIterator:
57+
58+
def __init__(self, compressedString: str):
59+
self.d = []
60+
self.p = 0
61+
n = len(compressedString)
62+
i = 0
63+
while i < n:
64+
c = compressedString[i]
65+
x = 0
66+
i += 1
67+
while i < n and compressedString[i].isdigit():
68+
x = x * 10 + int(compressedString[i])
69+
i += 1
70+
self.d.append([c, x])
71+
72+
def next(self) -> str:
73+
if not self.hasNext():
74+
return ' '
75+
ans = self.d[self.p][0]
76+
self.d[self.p][1] -= 1
77+
if self.d[self.p][1] == 0:
78+
self.p += 1
79+
return ans
80+
81+
def hasNext(self) -> bool:
82+
return self.p < len(self.d) and self.d[self.p][1] > 0
83+
84+
85+
# Your StringIterator object will be instantiated and called as such:
86+
# obj = StringIterator(compressedString)
87+
# param_1 = obj.next()
88+
# param_2 = obj.hasNext()
5789
```
5890

5991
### **Java**
6092

6193
```java
94+
class StringIterator {
95+
private List<Node> d = new ArrayList<>();
96+
private int p;
97+
98+
public StringIterator(String compressedString) {
99+
int n = compressedString.length();
100+
int i = 0;
101+
while (i < n) {
102+
char c = compressedString.charAt(i);
103+
int x = 0;
104+
while (++i < n && Character.isDigit(compressedString.charAt(i))) {
105+
x = x * 10 + (compressedString.charAt(i) - '0');
106+
}
107+
d.add(new Node(c, x));
108+
}
109+
}
110+
111+
public char next() {
112+
if (!hasNext()) {
113+
return ' ';
114+
}
115+
char ans = d.get(p).c;
116+
if (--d.get(p).x == 0) {
117+
++p;
118+
}
119+
return ans;
120+
}
121+
122+
public boolean hasNext() {
123+
return p < d.size() && d.get(p).x > 0;
124+
}
125+
}
126+
127+
class Node {
128+
char c;
129+
int x;
130+
131+
Node(char c, int x) {
132+
this.c = c;
133+
this.x = x;
134+
}
135+
}
136+
137+
/**
138+
* Your StringIterator object will be instantiated and called as such:
139+
* StringIterator obj = new StringIterator(compressedString);
140+
* char param_1 = obj.next();
141+
* boolean param_2 = obj.hasNext();
142+
*/
143+
```
144+
145+
### **C++**
146+
147+
```cpp
148+
class StringIterator {
149+
public:
150+
StringIterator(string compressedString) {
151+
int n = compressedString.size();
152+
int i = 0;
153+
while (i < n) {
154+
char c = compressedString[i];
155+
int x = 0;
156+
while (++i < n && isdigit(compressedString[i])) {
157+
x = x * 10 + (compressedString[i] - '0');
158+
}
159+
d.push_back({c, x});
160+
}
161+
}
162+
163+
char next() {
164+
if (!hasNext()) return ' ';
165+
char ans = d[p].first;
166+
if (--d[p].second == 0) {
167+
++p;
168+
}
169+
return ans;
170+
}
171+
172+
bool hasNext() {
173+
return p < d.size() && d[p].second > 0;
174+
}
175+
private:
176+
vector<pair<char, int>> d;
177+
int p = 0;
178+
};
179+
180+
/**
181+
* Your StringIterator object will be instantiated and called as such:
182+
* StringIterator* obj = new StringIterator(compressedString);
183+
* char param_1 = obj->next();
184+
* bool param_2 = obj->hasNext();
185+
*/
186+
```
62187
188+
### **Go**
189+
190+
```go
191+
type pair struct {
192+
c byte
193+
x int
194+
}
195+
196+
type StringIterator struct {
197+
d []pair
198+
p int
199+
}
200+
201+
func Constructor(compressedString string) StringIterator {
202+
n := len(compressedString)
203+
i := 0
204+
d := []pair{}
205+
for i < n {
206+
c := compressedString[i]
207+
x := 0
208+
i++
209+
for i < n && compressedString[i] >= '0' && compressedString[i] <= '9' {
210+
x = x*10 + int(compressedString[i]-'0')
211+
i++
212+
}
213+
d = append(d, pair{c, x})
214+
}
215+
return StringIterator{d, 0}
216+
}
217+
218+
func (this *StringIterator) Next() byte {
219+
if !this.HasNext() {
220+
return ' '
221+
}
222+
ans := this.d[this.p].c
223+
this.d[this.p].x--
224+
if this.d[this.p].x == 0 {
225+
this.p++
226+
}
227+
return ans
228+
}
229+
230+
func (this *StringIterator) HasNext() bool {
231+
return this.p < len(this.d) && this.d[this.p].x > 0
232+
}
233+
234+
/**
235+
* Your StringIterator object will be instantiated and called as such:
236+
* obj := Constructor(compressedString);
237+
* param_1 := obj.Next();
238+
* param_2 := obj.HasNext();
239+
*/
63240
```
64241

65242
### **...**

0 commit comments

Comments
 (0)