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

leetcode341

数据结构设计类题目,参考网上的代码:

/**
 * // 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:
    NestedIterator(vector<NestedInteger> &nestedList) {
        begins.push(nestedList.begin());
        ends.push(nestedList.end());
    }

    int next() {
        return (begins.top()++)->getInteger();
    }

    bool hasNext() {
        while(begins.size())        
        {            
            if(begins.top() == ends.top())            
            {                
                begins.pop();                
                ends.pop();            
            }            
            else            
            {                
                auto val = begins.top();                
                if(val->isInteger()) 
                    return true;                
                begins.top()++;                
                begins.push(val->getList().begin());                
                ends.push(val->getList().end());            
            }        
        }        
        return false;

    }

private:
    stack<vector<NestedInteger>::iterator> begins, ends;
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

 

转载于:https://www.cnblogs.com/asenyang/p/9739259.html

相关文章:

  • Java编译时出现的一个编译错误
  • citus实战系列之四多CN部署
  • mysqlbinlog恢复数据-update20140820
  • 转: 利用RabbitMQ、MySQL实现超大用户级别的消息在/离线收发
  • alibaba笔试2
  • python爬取网站数据,如何绕过反爬虫策略
  • [OGRE]看备注学编程(02):打地鼠01-布置场地九只地鼠
  • 巧用 TypeScript (一)
  • 谁在使用MongoDB
  • 精选Excel之Ctrl+26字母快捷键,职场办公人员必会!
  • mac下搭建java开发环境:eclipse+tomcat+maven
  • 分布式id生成
  • Android Day1
  • 插件和目标
  • 云信IMweb端开发总结
  • [case10]使用RSQL实现端到端的动态查询
  • C++11: atomic 头文件
  • Java|序列化异常StreamCorruptedException的解决方法
  • Java到底能干嘛?
  • js数组之filter
  • PHP面试之三:MySQL数据库
  • spring-boot List转Page
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 初识 webpack
  • 记一次和乔布斯合作最难忘的经历
  • 检测对象或数组
  • 简单易用的leetcode开发测试工具(npm)
  • 解析带emoji和链接的聊天系统消息
  • 你不可错过的前端面试题(一)
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 试着探索高并发下的系统架构面貌
  • 突破自己的技术思维
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 与 ConTeXt MkIV 官方文档的接驳
  • 在electron中实现跨域请求,无需更改服务器端设置
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • Prometheus VS InfluxDB
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • ###C语言程序设计-----C语言学习(3)#
  • #DBA杂记1
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (poj1.2.1)1970(筛选法模拟)
  • (笔试题)合法字符串
  • (二)PySpark3:SparkSQL编程
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)我也是一只IT小小鸟
  • .Net 8.0 新的变化
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .Net Memory Profiler的使用举例
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET 药厂业务系统 CPU爆高分析