[LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Example
Given the list [[1,1],2,[1,1]], By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
Given the list [1,[4,[6]]], By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
LeetCode上的原题,请参见我之前的博客Flatten Nested List Iterator。但是不太明白的是,那篇博客中的解法三可以通过LeetCode的OJ,在LintCode上跑就有错误,不知道啥原因。
解法一:
- class NestedIterator {
- public:
- NestedIterator(vector<NestedInteger> &nestedList) {
- for (int i = nestedList.size() - ; i >= ; --i) {
- s.push(nestedList[i]);
- }
- }
- int next() {
- NestedInteger t = s.top(); s.pop();
- return t.getInteger();
- }
- bool hasNext() {
- while (!s.empty()) {
- NestedInteger t = s.top();
- if (t.isInteger()) return true;
- s.pop();
- for (int i = t.getList().size() - ; i >= ; --i) {
- s.push(t.getList()[i]);
- }
- }
- return false;
- }
- private:
- stack<NestedInteger> s;
- };
解法二:
- class NestedIterator {
- public:
- NestedIterator(vector<NestedInteger> &nestedList) {
- for (auto a : nestedList) {
- d.push_back(a);
- }
- }
- int next() {
- NestedInteger t = d.front(); d.pop_front();
- return t.getInteger();
- }
- bool hasNext() {
- while (!d.empty()) {
- NestedInteger t = d.front();
- if (t.isInteger()) return true;
- d.pop_front();
- for (int i = ; i < t.getList().size(); ++i) {
- d.insert(d.begin() + i, t.getList()[i]);
- }
- }
- return false;
- }
- private:
- deque<NestedInteger> d;
- };
[LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器的更多相关文章
- [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [LeetCode] Nested List Weight Sum 嵌套链表权重和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- Leetcode: Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- Design-341. Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
随机推荐
- Sql Server 基础知识
Sql Server 基础知识: http://blog.csdn.net/t6786780/article/details/4525652 Sql Server 语句大全: http://www.c ...
- Linux内核system_call中断处理过程
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...
- 纯window下VMware 安装 OS X El Capitan 原版映像【未完待续】
一.所需软件1.下载OS X El Capitan 10.11.2 15C50链接:http://pan.baidu.com/s/1skuLgAx 密码:u2jf 2.下载VMware Worksta ...
- SQL.WITH AS.公用表表达式(CTE)(转)
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...
- directive中的参数详解
restrictE: 表示该directive仅能以element方式使用,即:<my-dialog></my-dialog>A: 表示该directive仅能以attribu ...
- poj3616 LIS变形
题目链接:http://poj.org/problem?id=3616 题意:给出m组数据a,b,c代表在第a分钟到第b分钟产生c个效益,问最大产生多少效益(区间不能重叠,每次工作完必须歇息R分钟) ...
- BZOJ 2342 回文串-Manacher
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2342 思路:先跑一遍Manacher求出p[i]为每个位置为中心的回文半径,因为双倍回文串 ...
- BZOJ 2648 SJY摆棋子 ——KD-Tree
[题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...
- css3 -- 多列
1.指定分列: E{column-count:2:} --- 两列 E{ -moz-column-count:2: -webkit-column-count:2: } Firefox与webkit实现 ...
- Git分支操作
1.创建分支 git branch <分支名> 2.切换分支 git checkout <分支名> 该语句和上一个语句可以和起来用一个语句表示: git checkout -b ...