@@ -41,6 +41,18 @@ iterator.hasNext(); // 返回 false
41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
+ ** 方法一:双指针**
45
+
46
+ 我们定义两个指针 $i$ 和 $j$,分别指向当前二维向量的行和列,初始时 $i = 0$,$j = 0$。
47
+
48
+ 接下来,我们设计一个函数 $forward()$,用于将 $i$ 和 $j$ 向后移动,直到指向一个非空的元素。
49
+
50
+ 每次调用 ` next ` 方法时,我们先调用 $forward()$,然后返回当前指向的元素,最后将 $j$ 向后移动一位。
51
+
52
+ 每次调用 ` hasNext ` 方法时,我们先调用 $forward()$,然后判断 $i$ 是否小于二维向量的行数,如果是,则返回 ` true ` ,否则返回 ` false ` 。
53
+
54
+ 时间复杂度 $O(1)$,空间复杂度 $O(1)$。
55
+
44
56
<!-- tabs:start -->
45
57
46
58
### ** Python3**
@@ -50,18 +62,24 @@ iterator.hasNext(); // 返回 false
50
62
``` python
51
63
class Vector2D :
52
64
def __init__ (self , vec : List[List[int ]]):
53
- self .flatten = []
54
- for item in vec:
55
- for e in item:
56
- self .flatten.append(e)
57
- self .cur = - 1
65
+ self .i = 0
66
+ self .j = 0
67
+ self .vec = vec
58
68
59
69
def next (self ) -> int :
60
- self .cur += 1
61
- return self .flatten[self .cur]
70
+ self .forward()
71
+ ans = self .vec[self .i][self .j]
72
+ self .j += 1
73
+ return ans
62
74
63
75
def hasNext (self ) -> bool :
64
- return self .cur < len (self .flatten) - 1
76
+ self .forward()
77
+ return self .i < len (self .vec)
78
+
79
+ def forward (self ):
80
+ while self .i < len (self .vec) and self .j >= len (self .vec[self .i]):
81
+ self .i += 1
82
+ self .j = 0
65
83
66
84
67
85
# Your Vector2D object will be instantiated and called as such:
@@ -75,7 +93,158 @@ class Vector2D:
75
93
<!-- 这里可写当前语言的特殊实现逻辑 -->
76
94
77
95
``` java
96
+ class Vector2D {
97
+ private int i;
98
+ private int j;
99
+ private int [][] vec;
100
+
101
+ public Vector2D (int [][] vec ) {
102
+ this . vec = vec;
103
+ }
104
+
105
+ public int next () {
106
+ forward();
107
+ return vec[i][j++ ];
108
+ }
109
+
110
+ public boolean hasNext () {
111
+ forward();
112
+ return i < vec. length;
113
+ }
114
+
115
+ private void forward () {
116
+ while (i < vec. length && j >= vec[i]. length) {
117
+ ++ i;
118
+ j = 0 ;
119
+ }
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Your Vector2D object will be instantiated and called as such:
125
+ * Vector2D obj = new Vector2D(vec);
126
+ * int param_1 = obj.next();
127
+ * boolean param_2 = obj.hasNext();
128
+ */
129
+ ```
130
+
131
+ ### ** C++**
132
+
133
+ ``` cpp
134
+ class Vector2D {
135
+ public:
136
+ Vector2D(vector<vector<int >>& vec) {
137
+ this->vec = move(vec);
138
+ }
139
+
140
+ int next() {
141
+ forward();
142
+ return vec[i][j++];
143
+ }
144
+
145
+ bool hasNext () {
146
+ forward ();
147
+ return i < vec.size();
148
+ }
149
+
150
+ private:
151
+ int i = 0;
152
+ int j = 0;
153
+ vector<vector<int >> vec;
154
+
155
+ void forward() {
156
+ while (i < vec.size() && j >= vec[i].size()) {
157
+ ++i;
158
+ j = 0;
159
+ }
160
+ }
161
+ };
162
+
163
+ /* *
164
+ * Your Vector2D object will be instantiated and called as such:
165
+ * Vector2D* obj = new Vector2D(vec);
166
+ * int param_1 = obj->next();
167
+ * bool param_2 = obj->hasNext();
168
+ */
169
+ ```
170
+
171
+ ### ** Go**
172
+
173
+ ``` go
174
+ type Vector2D struct {
175
+ i, j int
176
+ vec [][]int
177
+ }
178
+
179
+ func Constructor (vec [][]int ) Vector2D {
180
+ return Vector2D{vec: vec}
181
+ }
182
+
183
+ func (this *Vector2D ) Next () int {
184
+ this.forward ()
185
+ ans := this.vec [this.i ][this.j ]
186
+ this.j ++
187
+ return ans
188
+ }
189
+
190
+ func (this *Vector2D ) HasNext () bool {
191
+ this.forward ()
192
+ return this.i < len (this.vec )
193
+ }
194
+
195
+ func (this *Vector2D ) forward () {
196
+ for this.i < len (this.vec ) && this.j >= len (this.vec [this.i ]) {
197
+ this.i ++
198
+ this.j = 0
199
+ }
200
+ }
201
+
202
+ /* *
203
+ * Your Vector2D object will be instantiated and called as such:
204
+ * obj := Constructor(vec);
205
+ * param_1 := obj.Next();
206
+ * param_2 := obj.HasNext();
207
+ */
208
+ ```
209
+
210
+ ### ** TypeScript**
211
+
212
+ ``` ts
213
+ class Vector2D {
214
+ i: number ;
215
+ j: number ;
216
+ vec: number [][];
217
+
218
+ constructor (vec : number [][]) {
219
+ this .i = 0 ;
220
+ this .j = 0 ;
221
+ this .vec = vec ;
222
+ }
223
+
224
+ next(): number {
225
+ this .forward ();
226
+ return this .vec [this .i ][this .j ++ ];
227
+ }
228
+
229
+ hasNext(): boolean {
230
+ this .forward ();
231
+ return this .i < this .vec .length ;
232
+ }
233
+
234
+ forward(): void {
235
+ while (this .i < this .vec .length && this .j >= this .vec [this .i ].length ) {
236
+ ++ this .i ;
237
+ this .j = 0 ;
238
+ }
239
+ }
240
+ }
78
241
242
+ /**
243
+ * Your Vector2D object will be instantiated and called as such:
244
+ * var obj = new Vector2D(vec)
245
+ * var param_1 = obj.next()
246
+ * var param_2 = obj.hasNext()
247
+ */
79
248
```
80
249
81
250
### ** ...**
0 commit comments