Skip to content

Commit 532467e

Browse files
committed
feat: add solutions to lc problem: No.0900
No.0900.RLE Iterator
1 parent 8a947f3 commit 532467e

File tree

6 files changed

+372
-2
lines changed

6 files changed

+372
-2
lines changed

solution/0900-0999/0900.RLE Iterator/README.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,141 @@ RLEIterator 由 RLEIterator([3,8,0,9,2,5]) 初始化。
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```python
61-
61+
class RLEIterator:
62+
63+
def __init__(self, encoding: List[int]):
64+
self.encoding = encoding
65+
self.i = 0
66+
self.curr = 0
67+
68+
def next(self, n: int) -> int:
69+
while self.i < len(self.encoding):
70+
if self.curr + n > self.encoding[self.i]:
71+
n -= self.encoding[self.i] - self.curr
72+
self.curr = 0
73+
self.i += 2
74+
else:
75+
self.curr += n
76+
return self.encoding[self.i + 1]
77+
return -1
78+
79+
80+
# Your RLEIterator object will be instantiated and called as such:
81+
# obj = RLEIterator(encoding)
82+
# param_1 = obj.next(n)
6283
```
6384

6485
### **Java**
6586

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

6889
```java
90+
class RLEIterator {
91+
private int[] encoding;
92+
private int curr;
93+
private int i;
94+
95+
public RLEIterator(int[] encoding) {
96+
this.encoding = encoding;
97+
curr = 0;
98+
i = 0;
99+
}
100+
101+
public int next(int n) {
102+
while (i < encoding.length) {
103+
if (curr + n > encoding[i]) {
104+
n -= encoding[i] - curr;
105+
i += 2;
106+
curr = 0;
107+
} else {
108+
curr += n;
109+
return encoding[i + 1];
110+
}
111+
}
112+
return -1;
113+
}
114+
}
115+
116+
/**
117+
* Your RLEIterator object will be instantiated and called as such:
118+
* RLEIterator obj = new RLEIterator(encoding);
119+
* int param_1 = obj.next(n);
120+
*/
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class RLEIterator {
127+
public:
128+
vector<int> encoding;
129+
int curr;
130+
int i;
131+
132+
RLEIterator(vector<int>& encoding) {
133+
this->encoding = encoding;
134+
this->curr = 0;
135+
this->i = 0;
136+
}
137+
138+
int next(int n) {
139+
while (i < encoding.size())
140+
{
141+
if (curr + n > encoding[i])
142+
{
143+
n -= encoding[i] - curr;
144+
curr = 0;
145+
i += 2;
146+
}
147+
else
148+
{
149+
curr += n;
150+
return encoding[i + 1];
151+
}
152+
}
153+
return -1;
154+
}
155+
};
156+
157+
/**
158+
* Your RLEIterator object will be instantiated and called as such:
159+
* RLEIterator* obj = new RLEIterator(encoding);
160+
* int param_1 = obj->next(n);
161+
*/
162+
```
69163
164+
### **Go**
165+
166+
```go
167+
type RLEIterator struct {
168+
encoding []int
169+
curr int
170+
i int
171+
}
172+
173+
func Constructor(encoding []int) RLEIterator {
174+
return RLEIterator{encoding: encoding, curr: 0, i: 0}
175+
}
176+
177+
func (this *RLEIterator) Next(n int) int {
178+
for this.i < len(this.encoding) {
179+
if this.curr+n > this.encoding[this.i] {
180+
n -= this.encoding[this.i] - this.curr
181+
this.curr = 0
182+
this.i += 2
183+
} else {
184+
this.curr += n
185+
return this.encoding[this.i+1]
186+
}
187+
}
188+
return -1
189+
}
190+
191+
/**
192+
* Your RLEIterator object will be instantiated and called as such:
193+
* obj := Constructor(encoding);
194+
* param_1 := obj.Next(n);
195+
*/
70196
```
71197

72198
### **...**

solution/0900-0999/0900.RLE Iterator/README_EN.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,139 @@ but the second term did not exist. Since the last term exhausted does not exist
6969
### **Python3**
7070

7171
```python
72-
72+
class RLEIterator:
73+
74+
def __init__(self, encoding: List[int]):
75+
self.encoding = encoding
76+
self.i = 0
77+
self.curr = 0
78+
79+
def next(self, n: int) -> int:
80+
while self.i < len(self.encoding):
81+
if self.curr + n > self.encoding[self.i]:
82+
n -= self.encoding[self.i] - self.curr
83+
self.curr = 0
84+
self.i += 2
85+
else:
86+
self.curr += n
87+
return self.encoding[self.i + 1]
88+
return -1
89+
90+
91+
# Your RLEIterator object will be instantiated and called as such:
92+
# obj = RLEIterator(encoding)
93+
# param_1 = obj.next(n)
7394
```
7495

7596
### **Java**
7697

7798
```java
99+
class RLEIterator {
100+
private int[] encoding;
101+
private int curr;
102+
private int i;
103+
104+
public RLEIterator(int[] encoding) {
105+
this.encoding = encoding;
106+
curr = 0;
107+
i = 0;
108+
}
109+
110+
public int next(int n) {
111+
while (i < encoding.length) {
112+
if (curr + n > encoding[i]) {
113+
n -= encoding[i] - curr;
114+
i += 2;
115+
curr = 0;
116+
} else {
117+
curr += n;
118+
return encoding[i + 1];
119+
}
120+
}
121+
return -1;
122+
}
123+
}
124+
125+
/**
126+
* Your RLEIterator object will be instantiated and called as such:
127+
* RLEIterator obj = new RLEIterator(encoding);
128+
* int param_1 = obj.next(n);
129+
*/
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class RLEIterator {
136+
public:
137+
vector<int> encoding;
138+
int curr;
139+
int i;
140+
141+
RLEIterator(vector<int>& encoding) {
142+
this->encoding = encoding;
143+
this->curr = 0;
144+
this->i = 0;
145+
}
146+
147+
int next(int n) {
148+
while (i < encoding.size())
149+
{
150+
if (curr + n > encoding[i])
151+
{
152+
n -= encoding[i] - curr;
153+
curr = 0;
154+
i += 2;
155+
}
156+
else
157+
{
158+
curr += n;
159+
return encoding[i + 1];
160+
}
161+
}
162+
return -1;
163+
}
164+
};
165+
166+
/**
167+
* Your RLEIterator object will be instantiated and called as such:
168+
* RLEIterator* obj = new RLEIterator(encoding);
169+
* int param_1 = obj->next(n);
170+
*/
171+
```
78172
173+
### **Go**
174+
175+
```go
176+
type RLEIterator struct {
177+
encoding []int
178+
curr int
179+
i int
180+
}
181+
182+
func Constructor(encoding []int) RLEIterator {
183+
return RLEIterator{encoding: encoding, curr: 0, i: 0}
184+
}
185+
186+
func (this *RLEIterator) Next(n int) int {
187+
for this.i < len(this.encoding) {
188+
if this.curr+n > this.encoding[this.i] {
189+
n -= this.encoding[this.i] - this.curr
190+
this.curr = 0
191+
this.i += 2
192+
} else {
193+
this.curr += n
194+
return this.encoding[this.i+1]
195+
}
196+
}
197+
return -1
198+
}
199+
200+
/**
201+
* Your RLEIterator object will be instantiated and called as such:
202+
* obj := Constructor(encoding);
203+
* param_1 := obj.Next(n);
204+
*/
79205
```
80206

81207
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class RLEIterator {
2+
public:
3+
vector<int> encoding;
4+
int curr;
5+
int i;
6+
7+
RLEIterator(vector<int>& encoding) {
8+
this->encoding = encoding;
9+
this->curr = 0;
10+
this->i = 0;
11+
}
12+
13+
int next(int n) {
14+
while (i < encoding.size())
15+
{
16+
if (curr + n > encoding[i])
17+
{
18+
n -= encoding[i] - curr;
19+
curr = 0;
20+
i += 2;
21+
}
22+
else
23+
{
24+
curr += n;
25+
return encoding[i + 1];
26+
}
27+
}
28+
return -1;
29+
}
30+
};
31+
32+
/**
33+
* Your RLEIterator object will be instantiated and called as such:
34+
* RLEIterator* obj = new RLEIterator(encoding);
35+
* int param_1 = obj->next(n);
36+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
type RLEIterator struct {
2+
encoding []int
3+
curr int
4+
i int
5+
}
6+
7+
func Constructor(encoding []int) RLEIterator {
8+
return RLEIterator{encoding: encoding, curr: 0, i: 0}
9+
}
10+
11+
func (this *RLEIterator) Next(n int) int {
12+
for this.i < len(this.encoding) {
13+
if this.curr+n > this.encoding[this.i] {
14+
n -= this.encoding[this.i] - this.curr
15+
this.curr = 0
16+
this.i += 2
17+
} else {
18+
this.curr += n
19+
return this.encoding[this.i+1]
20+
}
21+
}
22+
return -1
23+
}
24+
25+
/**
26+
* Your RLEIterator object will be instantiated and called as such:
27+
* obj := Constructor(encoding);
28+
* param_1 := obj.Next(n);
29+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class RLEIterator {
2+
private int[] encoding;
3+
private int curr;
4+
private int i;
5+
6+
public RLEIterator(int[] encoding) {
7+
this.encoding = encoding;
8+
curr = 0;
9+
i = 0;
10+
}
11+
12+
public int next(int n) {
13+
while (i < encoding.length) {
14+
if (curr + n > encoding[i]) {
15+
n -= encoding[i] - curr;
16+
i += 2;
17+
curr = 0;
18+
} else {
19+
curr += n;
20+
return encoding[i + 1];
21+
}
22+
}
23+
return -1;
24+
}
25+
}
26+
27+
/**
28+
* Your RLEIterator object will be instantiated and called as such:
29+
* RLEIterator obj = new RLEIterator(encoding);
30+
* int param_1 = obj.next(n);
31+
*/

0 commit comments

Comments
 (0)