@@ -58,15 +58,141 @@ RLEIterator 由 RLEIterator([3,8,0,9,2,5]) 初始化。
58
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
59
60
60
``` 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)
62
83
```
63
84
64
85
### ** Java**
65
86
66
87
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
88
68
89
``` 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
+ ```
69
163
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
+ */
70
196
```
71
197
72
198
### ** ...**
0 commit comments