반응형
https://leetcode.com/problems/flatten-nested-list-iterator/
재귀를 이용하여 1차원 배열로 초기화시켜주었습니다.
class NestedIterator {
public:
vector<int> values;
int idx = 0;
NestedIterator(vector<NestedInteger> &nestedList) {
init(nestedList);
}
int next() {
return values[idx++];
}
bool hasNext() {
return idx < values.size();
}
private:
void init(vector<NestedInteger>& nestedList) {
for(auto& nested : nestedList) {
if(nested.isInteger()) {
values.push_back(nested.getInteger());
} else {
init(nested.getList());
}
}
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1641 : Count Sorted Vowel Strings (0) | 2022.05.11 |
---|---|
LeetCode 17 : Letter Combinations of a Phone Number (0) | 2022.05.09 |
LeetCode 456 : 132 Pattern (0) | 2022.05.07 |
LeetCode 1209 : Remove All Adjacent Duplicates in String II (0) | 2022.05.06 |
LeetCode 225 : Implement Stack using Queues (0) | 2022.05.05 |