Skip to content

Commit 91551ae

Browse files
committed
feat: solve No.341,844
1 parent db863bf commit 91551ae

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# 341. Flatten Nested List Iterator
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Stack, Tree, Depth-First Search, Design, Queue, Iterator.
5+
- Similar Questions: Flatten 2D Vector, Zigzag Iterator, Mini Parser, Array Nesting.
6+
7+
## Problem
8+
9+
You are given a nested list of integers `nestedList`. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
10+
11+
Implement the `NestedIterator` class:
12+
13+
14+
15+
- `NestedIterator(List<NestedInteger> nestedList)` Initializes the iterator with the nested list `nestedList`.
16+
17+
- `int next()` Returns the next integer in the nested list.
18+
19+
- `boolean hasNext()` Returns `true` if there are still some integers in the nested list and `false` otherwise.
20+
21+
22+
Your code will be tested with the following pseudocode:
23+
24+
```
25+
initialize iterator with nestedList
26+
res = []
27+
while iterator.hasNext()
28+
append iterator.next() to the end of res
29+
return res
30+
```
31+
32+
If `res` matches the expected flattened list, then your code will be judged as correct.
33+
34+
 
35+
Example 1:
36+
37+
```
38+
Input: nestedList = [[1,1],2,[1,1]]
39+
Output: [1,1,2,1,1]
40+
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
41+
```
42+
43+
Example 2:
44+
45+
```
46+
Input: nestedList = [1,[4,[6]]]
47+
Output: [1,4,6]
48+
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
49+
```
50+
51+
 
52+
**Constraints:**
53+
54+
55+
56+
- `1 <= nestedList.length <= 500`
57+
58+
- The values of the integers in the nested list is in the range `[-106, 106]`.
59+
60+
61+
62+
## Solution
63+
64+
```javascript
65+
/**
66+
* // This is the interface that allows for creating nested lists.
67+
* // You should not implement it, or speculate about its implementation
68+
* function NestedInteger() {
69+
*
70+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
71+
* @return {boolean}
72+
* this.isInteger = function() {
73+
* ...
74+
* };
75+
*
76+
* Return the single integer that this NestedInteger holds, if it holds a single integer
77+
* Return null if this NestedInteger holds a nested list
78+
* @return {integer}
79+
* this.getInteger = function() {
80+
* ...
81+
* };
82+
*
83+
* Return the nested list that this NestedInteger holds, if it holds a nested list
84+
* Return null if this NestedInteger holds a single integer
85+
* @return {NestedInteger[]}
86+
* this.getList = function() {
87+
* ...
88+
* };
89+
* };
90+
*/
91+
/**
92+
* @constructor
93+
* @param {NestedInteger[]} nestedList
94+
*/
95+
var NestedIterator = function(nestedList) {
96+
this.index = 0;
97+
this.list = [];
98+
const flatten = (list) => {
99+
for (var item of list) {
100+
if (item.isInteger()) {
101+
this.list.push(item.getInteger());
102+
} else {
103+
flatten(item.getList());
104+
}
105+
}
106+
};
107+
flatten(nestedList);
108+
};
109+
110+
111+
/**
112+
* @this NestedIterator
113+
* @returns {boolean}
114+
*/
115+
NestedIterator.prototype.hasNext = function() {
116+
return this.index < this.list.length;
117+
};
118+
119+
/**
120+
* @this NestedIterator
121+
* @returns {integer}
122+
*/
123+
NestedIterator.prototype.next = function() {
124+
return this.list[this.index++];
125+
};
126+
127+
/**
128+
* Your NestedIterator will be called like this:
129+
* var i = new NestedIterator(nestedList), a = [];
130+
* while (i.hasNext()) a.push(i.next());
131+
*/
132+
```
133+
134+
**Explain:**
135+
136+
nope.
137+
138+
**Complexity:**
139+
140+
* Time complexity : O(n).
141+
* Space complexity : O(n).
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 844. Backspace String Compare
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Two Pointers, String, Stack, Simulation.
5+
- Similar Questions: Crawler Log Folder, Removing Stars From a String.
6+
7+
## Problem
8+
9+
Given two strings `s` and `t`, return `true` **if they are equal when both are typed into empty text editors**. `'#'` means a backspace character.
10+
11+
Note that after backspacing an empty text, the text will continue empty.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: s = "ab#c", t = "ad#c"
18+
Output: true
19+
Explanation: Both s and t become "ac".
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: s = "ab##", t = "c#d#"
26+
Output: true
27+
Explanation: Both s and t become "".
28+
```
29+
30+
Example 3:
31+
32+
```
33+
Input: s = "a#c", t = "b"
34+
Output: false
35+
Explanation: s becomes "c" while t becomes "b".
36+
```
37+
38+
 
39+
**Constraints:**
40+
41+
42+
43+
- `1 <= s.length, t.length <= 200`
44+
45+
- `s` and `t` only contain lowercase letters and `'#'` characters.
46+
47+
48+
 
49+
**Follow up:** Can you solve it in `O(n)` time and `O(1)` space?
50+
51+
52+
## Solution
53+
54+
```javascript
55+
/**
56+
* @param {string} s
57+
* @param {string} t
58+
* @return {boolean}
59+
*/
60+
var backspaceCompare = function(s, t) {
61+
var i = s.length - 1;
62+
var j = t.length - 1;
63+
while (i >= 0 || j >= 0) {
64+
i = findCharIndex(s, i);
65+
j = findCharIndex(t, j);
66+
if (s[i] !== t[j]) return false;
67+
i--;
68+
j--;
69+
}
70+
return true;
71+
};
72+
73+
var findCharIndex = function(s, i) {
74+
var num = 0;
75+
while (num || s[i] === '#') {
76+
s[i] === '#' ? num++ : num--;
77+
i--;
78+
}
79+
return i;
80+
}
81+
```
82+
83+
**Explain:**
84+
85+
nope.
86+
87+
**Complexity:**
88+
89+
* Time complexity : O(n).
90+
* Space complexity : O(1).

0 commit comments

Comments
 (0)