当前位置: 首页 > news >正文

琼海做网站口碑/sem托管公司

琼海做网站口碑,sem托管公司,wordpress社区论坛模板,淘宝小网站怎么做的注: 题目: 给你一个嵌套的整数列表 nestedList 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。 实现扁平迭代…

注:

题目:
给你一个嵌套的整数列表 nestedList 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。

实现扁平迭代器类 NestedIterator :

NestedIterator(List nestedList) 用嵌套列表 nestedList 初始化迭代器。
int next() 返回嵌套列表的下一个整数。
boolean hasNext() 如果仍然存在待迭代的整数,返回 true ;否则,返回 false 。
你的代码将会用下述伪代码检测:

initialize iterator with nestedList
res = []
while iterator.hasNext()append iterator.next() to the end of res
return res

如果 res 与预期的扁平化列表匹配,那么你的代码将会被判为正确。

示例 1:
输入:nestedList = [[1,1],2,[1,1]]
输出:[1,1,2,1,1]
解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,1,2,1,1]。
示例 2:
输入:nestedList = [1,[4,[6]]]
输出:[1,4,6]
解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,4,6]。

提示:
1 <= nestedList.length <= 500
嵌套列表中的整数值在范围 [-106, 106] 内

题解:
方法一 dfs
思路
嵌套的整型列表是一个树形结构,树上的叶子节点对应一个整数,非叶节点对应一个列表。

在这棵树上深度优先搜索的顺序就是迭代器遍历的顺序。

我们可以先遍历整个嵌套列表,将所有整数存入一个数组,然后遍历该数组从而实现 next 和 hasNext 方法。

复杂度分析
时间复杂度:初始化为 O(n),next 和 hasNext 为 O(1)。其中 nn 是嵌套的整型列表中的元素个数。

空间复杂度:O(n)。需要一个数组存储嵌套的整型列表中的所有元素。

/*** // This is the interface that allows for creating nested lists.* // You should not implement it, or speculate about its implementation* class NestedInteger {*   public:*     // Return true if this NestedInteger holds a single integer, rather than a nested list.*     bool isInteger() const;**     // Return the single integer that this NestedInteger holds, if it holds a single integer*     // The result is undefined if this NestedInteger holds a nested list*     int getInteger() const;**     // Return the nested list that this NestedInteger holds, if it holds a nested list*     // The result is undefined if this NestedInteger holds a single integer*     const vector<NestedInteger> &getList() const;* };*/class NestedIterator {
public:vector<int> result;vector<int>::iterator cur;void dfs(vector<NestedInteger> lists){for(auto list:lists){if(list.isInteger()){result.push_back(list.getInteger());}else{dfs(list.getList());}}} NestedIterator(vector<NestedInteger> &nestedList) {dfs(nestedList);cur=result.begin();}int next() {return *(cur++);}bool hasNext() {return cur!=result.end();}
};/*** Your NestedIterator object will be instantiated and called as such:* NestedIterator i(nestedList);* while (i.hasNext()) cout << i.next();*/

方法二 栈
思路
我们可以用一个栈来代替方法一中的递归过程。

具体来说,用一个栈来维护深度优先搜索时,从根节点到当前节点路径上的所有节点。由于非叶节点对应的是一个列表,我们在栈中存储的是指向列表当前遍历的元素的指针(下标)。每次向下搜索时,取出列表的当前指针指向的元素并将其入栈,同时将该指针向后移动一位。如此反复直到找到一个整数。循环时若栈顶指针指向了列表末尾,则将其从栈顶弹出。

复杂度分析
时间复杂度:初始化和next 为 O(1),hasNext 为均摊 O(1)。

空间复杂度:O(n)。最坏情况下嵌套的整型列表是一条链,我们需要一个 O(n) 大小的栈来存储链上的所有元素。

/*** // This is the interface that allows for creating nested lists.* // You should not implement it, or speculate about its implementation* class NestedInteger {*   public:*     // Return true if this NestedInteger holds a single integer, rather than a nested list.*     bool isInteger() const;**     // Return the single integer that this NestedInteger holds, if it holds a single integer*     // The result is undefined if this NestedInteger holds a nested list*     int getInteger() const;**     // Return the nested list that this NestedInteger holds, if it holds a nested list*     // The result is undefined if this NestedInteger holds a single integer*     const vector<NestedInteger> &getList() const;* };*/class NestedIterator {
public:stack<pair<vector<NestedInteger>::iterator,vector<NestedInteger>::iterator>> stk;NestedIterator(vector<NestedInteger> &nestedList) {stk.emplace(nestedList.begin(),nestedList.end());}int next() {return stk.top().first++->getInteger();}bool hasNext() {while(!stk.empty()){pair<vector<NestedInteger>::iterator,vector<NestedInteger>::iterator> &p=stk.top();if(p.first==p.second){stk.pop();continue;}// 若当前元素为整数,则返回trueif(p.first->isInteger()==true){return true;}// 若当前元素为列表,则将其入栈,且迭代器p指向下一个元素vector<NestedInteger> &t=p.first++->getList();stk.emplace(t.begin(),t.end());}return false;}
};/*** Your NestedIterator object will be instantiated and called as such:* NestedIterator i(nestedList);* while (i.hasNext()) cout << i.next();*/
http://www.jmfq.cn/news/5299723.html

相关文章:

  • 鸡西百姓网免费发布信息网/如何利用seo赚钱
  • 河南省示范校建设专题网站/百度关键词推广可以自己做吗
  • 网站开发服务合同范本/哈尔滨seo网站管理
  • 免费自学平面设计的网站/网页游戏
  • 画册设计免费模板/seo流量工具
  • 广州优化网站推广/淘宝交易指数换算工具
  • 如何做网络推广员/广东百度seo
  • 做网站推销手表/做优化的网站
  • 重庆网站房地产/seo知识总结
  • 广告设计公司专业vi设计公司/seo网络贸易网站推广
  • 广州荔湾网站建设/软文推广案例
  • 外贸网站建设 福田/百度搜索引擎关键词
  • 河间做网站价格/今天刚刚发生的重大新闻
  • 佛山网站优化质量好/网络营销案例具体分析
  • 网站建设好的/国内新闻最近新闻今天
  • 网站建设合作合同/苏州seo按天扣费
  • 网站设计做图工具/外包网络推广公司
  • 高清品牌网站设计建设/铜陵seo
  • 钓鱼网站制作全套/网站测速
  • 温州建设小学的网站/网站开发流程的8个步骤
  • 上海网站建设系/杭州网站定制
  • 做网站优化词怎么选择/seo实战培训费用
  • 做本地生活圈网站好吗/欧美网站建设
  • 无锡高端网站建设/免费友情链接
  • 青岛营销型网站建设/南京seo报价
  • 最靠谱的海外购物网站/seo技术是什么意思
  • dz论坛可以做商业网站/微信广告投放推广平台
  • 电子商务网站关键技术/网上卖货的平台有哪些
  • 专门做淘宝优惠券的网站/一键优化清理加速
  • 手机站喝茶影视/baidu优化