Skip to content

Commit e3c1d5e

Browse files
committed
feat: add solutions to lc problems: No.0341,1423
- No.0341.Flatten Nested List Iterator - No.1423.Maximum Points You Can Obtain from Cards
1 parent 1a3032c commit e3c1d5e

File tree

8 files changed

+484
-0
lines changed

8 files changed

+484
-0
lines changed

solution/0300-0399/0341.Flatten Nested List Iterator/README.md

+136
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,142 @@ public class NestedIterator implements Iterator<Integer> {
174174
*/
175175
```
176176

177+
### **TypeScript**
178+
179+
```ts
180+
/**
181+
* // This is the interface that allows for creating nested lists.
182+
* // You should not implement it, or speculate about its implementation
183+
* class NestedInteger {
184+
* If value is provided, then it holds a single integer
185+
* Otherwise it holds an empty nested list
186+
* constructor(value?: number) {
187+
* ...
188+
* };
189+
*
190+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
191+
* isInteger(): boolean {
192+
* ...
193+
* };
194+
*
195+
* Return the single integer that this NestedInteger holds, if it holds a single integer
196+
* Return null if this NestedInteger holds a nested list
197+
* getInteger(): number | null {
198+
* ...
199+
* };
200+
*
201+
* Set this NestedInteger to hold a single integer equal to value.
202+
* setInteger(value: number) {
203+
* ...
204+
* };
205+
*
206+
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
207+
* add(elem: NestedInteger) {
208+
* ...
209+
* };
210+
*
211+
* Return the nested list that this NestedInteger holds,
212+
* or an empty list if this NestedInteger holds a single integer
213+
* getList(): NestedInteger[] {
214+
* ...
215+
* };
216+
* };
217+
*/
218+
219+
class NestedIterator {
220+
private vals: number[];
221+
private index: number;
222+
223+
constructor(nestedList: NestedInteger[]) {
224+
this.index = 0;
225+
this.vals = [];
226+
this.dfs(nestedList);
227+
}
228+
229+
dfs(nestedList: NestedInteger[]) {
230+
for (const v of nestedList) {
231+
if (v.isInteger()) {
232+
this.vals.push(v.getInteger());
233+
} else {
234+
this.dfs(v.getList());
235+
}
236+
}
237+
}
238+
239+
hasNext(): boolean {
240+
return this.index < this.vals.length;
241+
}
242+
243+
next(): number {
244+
return this.vals[this.index++];
245+
}
246+
}
247+
248+
/**
249+
* Your ParkingSystem object will be instantiated and called as such:
250+
* var obj = new NestedIterator(nestedList)
251+
* var a: number[] = []
252+
* while (obj.hasNext()) a.push(obj.next());
253+
*/
254+
```
255+
256+
### **Rust**
257+
258+
```rust
259+
// #[derive(Debug, PartialEq, Eq)]
260+
// pub enum NestedInteger {
261+
// Int(i32),
262+
// List(Vec<NestedInteger>)
263+
// }
264+
struct NestedIterator {
265+
index: usize,
266+
vals: Vec<i32>,
267+
}
268+
269+
270+
/**
271+
* `&self` means the method takes an immutable reference.
272+
* If you need a mutable reference, change it to `&mut self` instead.
273+
*/
274+
impl NestedIterator {
275+
276+
fn dfs(nestedList: &Vec<NestedInteger>, vals: &mut Vec<i32>) {
277+
for ele in nestedList.iter() {
278+
match ele {
279+
NestedInteger::Int(val) => vals.push(*val),
280+
NestedInteger::List(list) => Self::dfs(list, vals),
281+
}
282+
}
283+
}
284+
285+
fn new(nestedList: Vec<NestedInteger>) -> Self {
286+
let mut vals = vec![];
287+
Self::dfs(&nestedList, &mut vals);
288+
Self {
289+
vals,
290+
index: 0,
291+
}
292+
}
293+
294+
fn next(&mut self) -> i32 {
295+
let res = self.vals[self.index];
296+
self.index += 1;
297+
res
298+
}
299+
300+
fn has_next(&self) -> bool {
301+
self.index < self.vals.len()
302+
}
303+
}
304+
305+
/**
306+
* Your NestedIterator object will be instantiated and called as such:
307+
* let obj = NestedIterator::new(nestedList);
308+
* let ret_1: i32 = obj.next();
309+
* let ret_2: bool = obj.has_next();
310+
*/
311+
```
312+
177313
### **...**
178314

179315
```

solution/0300-0399/0341.Flatten Nested List Iterator/README_EN.md

+136
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,142 @@ public class NestedIterator implements Iterator<Integer> {
166166
*/
167167
```
168168

169+
### **TypeScript**
170+
171+
```ts
172+
/**
173+
* // This is the interface that allows for creating nested lists.
174+
* // You should not implement it, or speculate about its implementation
175+
* class NestedInteger {
176+
* If value is provided, then it holds a single integer
177+
* Otherwise it holds an empty nested list
178+
* constructor(value?: number) {
179+
* ...
180+
* };
181+
*
182+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
183+
* isInteger(): boolean {
184+
* ...
185+
* };
186+
*
187+
* Return the single integer that this NestedInteger holds, if it holds a single integer
188+
* Return null if this NestedInteger holds a nested list
189+
* getInteger(): number | null {
190+
* ...
191+
* };
192+
*
193+
* Set this NestedInteger to hold a single integer equal to value.
194+
* setInteger(value: number) {
195+
* ...
196+
* };
197+
*
198+
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
199+
* add(elem: NestedInteger) {
200+
* ...
201+
* };
202+
*
203+
* Return the nested list that this NestedInteger holds,
204+
* or an empty list if this NestedInteger holds a single integer
205+
* getList(): NestedInteger[] {
206+
* ...
207+
* };
208+
* };
209+
*/
210+
211+
class NestedIterator {
212+
private vals: number[];
213+
private index: number;
214+
215+
constructor(nestedList: NestedInteger[]) {
216+
this.index = 0;
217+
this.vals = [];
218+
this.dfs(nestedList);
219+
}
220+
221+
dfs(nestedList: NestedInteger[]) {
222+
for (const v of nestedList) {
223+
if (v.isInteger()) {
224+
this.vals.push(v.getInteger());
225+
} else {
226+
this.dfs(v.getList());
227+
}
228+
}
229+
}
230+
231+
hasNext(): boolean {
232+
return this.index < this.vals.length;
233+
}
234+
235+
next(): number {
236+
return this.vals[this.index++];
237+
}
238+
}
239+
240+
/**
241+
* Your ParkingSystem object will be instantiated and called as such:
242+
* var obj = new NestedIterator(nestedList)
243+
* var a: number[] = []
244+
* while (obj.hasNext()) a.push(obj.next());
245+
*/
246+
```
247+
248+
### **Rust**
249+
250+
```rust
251+
// #[derive(Debug, PartialEq, Eq)]
252+
// pub enum NestedInteger {
253+
// Int(i32),
254+
// List(Vec<NestedInteger>)
255+
// }
256+
struct NestedIterator {
257+
index: usize,
258+
vals: Vec<i32>,
259+
}
260+
261+
262+
/**
263+
* `&self` means the method takes an immutable reference.
264+
* If you need a mutable reference, change it to `&mut self` instead.
265+
*/
266+
impl NestedIterator {
267+
268+
fn dfs(nestedList: &Vec<NestedInteger>, vals: &mut Vec<i32>) {
269+
for ele in nestedList.iter() {
270+
match ele {
271+
NestedInteger::Int(val) => vals.push(*val),
272+
NestedInteger::List(list) => Self::dfs(list, vals),
273+
}
274+
}
275+
}
276+
277+
fn new(nestedList: Vec<NestedInteger>) -> Self {
278+
let mut vals = vec![];
279+
Self::dfs(&nestedList, &mut vals);
280+
Self {
281+
vals,
282+
index: 0,
283+
}
284+
}
285+
286+
fn next(&mut self) -> i32 {
287+
let res = self.vals[self.index];
288+
self.index += 1;
289+
res
290+
}
291+
292+
fn has_next(&self) -> bool {
293+
self.index < self.vals.len()
294+
}
295+
}
296+
297+
/**
298+
* Your NestedIterator object will be instantiated and called as such:
299+
* let obj = NestedIterator::new(nestedList);
300+
* let ret_1: i32 = obj.next();
301+
* let ret_2: bool = obj.has_next();
302+
*/
303+
```
304+
169305
### **...**
170306

171307
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// #[derive(Debug, PartialEq, Eq)]
2+
// pub enum NestedInteger {
3+
// Int(i32),
4+
// List(Vec<NestedInteger>)
5+
// }
6+
struct NestedIterator {
7+
index: usize,
8+
vals: Vec<i32>,
9+
}
10+
11+
12+
/**
13+
* `&self` means the method takes an immutable reference.
14+
* If you need a mutable reference, change it to `&mut self` instead.
15+
*/
16+
impl NestedIterator {
17+
18+
fn dfs(nestedList: &Vec<NestedInteger>, vals: &mut Vec<i32>) {
19+
for ele in nestedList.iter() {
20+
match ele {
21+
NestedInteger::Int(val) => vals.push(*val),
22+
NestedInteger::List(list) => Self::dfs(list, vals),
23+
}
24+
}
25+
}
26+
27+
fn new(nestedList: Vec<NestedInteger>) -> Self {
28+
let mut vals = vec![];
29+
Self::dfs(&nestedList, &mut vals);
30+
Self {
31+
vals,
32+
index: 0,
33+
}
34+
}
35+
36+
fn next(&mut self) -> i32 {
37+
let res = self.vals[self.index];
38+
self.index += 1;
39+
res
40+
}
41+
42+
fn has_next(&self) -> bool {
43+
self.index < self.vals.len()
44+
}
45+
}
46+
47+
/**
48+
* Your NestedIterator object will be instantiated and called as such:
49+
* let obj = NestedIterator::new(nestedList);
50+
* let ret_1: i32 = obj.next();
51+
* let ret_2: bool = obj.has_next();
52+
*/

0 commit comments

Comments
 (0)