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

[LeetCode]284. Peeking Iterator(C++,类,暴力)

题目链接:https://leetcode.com/problems/peeking-iterator/#/description

题意:实现个迭代器。

PeekingIterator是由Iterator继承来的,那么可以直接使用父类的hasNext和next。

关键是如何实现peek了,如何才能不迭代下去,但是可以看下一个值?

复制一下当前迭代器额。。。

 1 // Below is the interface for Iterator, which is already defined for you.
 2 // **DO NOT** modify the interface for Iterator.
 3 class Iterator {
 4     struct Data;
 5     Data* data;
 6 public:
 7     Iterator(const vector<int>& nums);
 8     Iterator(const Iterator& iter);
 9     virtual ~Iterator();
10     // Returns the next element in the iteration.
11     int next();
12     // Returns true if the iteration has more elements.
13     bool hasNext() const;
14 };
15 
16 
17 class PeekingIterator : public Iterator {
18 public:
19     PeekingIterator(const vector<int>& nums) : Iterator(nums) {
20         // Initialize any member here.
21         // **DO NOT** save a copy of nums and manipulate it directly.
22         // You should only use the Iterator interface methods.
23     }
24 
25     // Returns the next element in the iteration without advancing the iterator.
26     int peek() {
27         PeekingIterator it = *this;
28         return it.next();
29     }
30 
31     // hasNext() and next() should behave the same as in the Iterator interface.
32     // Override them if needed.
33     int next() {
34         return Iterator::next();
35     }
36 
37     bool hasNext() const {
38         return Iterator::hasNext();
39     }
40 };

 

转载于:https://www.cnblogs.com/kirai/p/6550753.html

相关文章:

  • SQL Server——海量数据库的查询优化及分页算法方案
  • 什么样的代码才是好代码
  • prototype 常用方法
  • 构建高质量的C#代码
  • C#的Enum—枚举
  • python 常用模块
  • 比较有用的枚举方法
  • 长训总结
  • 地形编辑工具
  • 怎么在企业管理器中得到执行一个select语句的毫秒级时间
  • Wireshark抓包过滤
  • 关于Remoting的个人使用心得
  • 工作不顺心怎么办?——Leo网上答疑(57)
  • 操作系统版本
  • SQL中 NVARCHAR 和 VARCHAR 两者之间的优势与劣势
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 【技术性】Search知识
  • Docker下部署自己的LNMP工作环境
  • learning koa2.x
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • miaov-React 最佳入门
  • Mithril.js 入门介绍
  • MYSQL 的 IF 函数
  • React-生命周期杂记
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 京东美团研发面经
  • 前端学习笔记之观察者模式
  • 思否第一天
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • 移动端高清、多屏适配方案
  • ​Python 3 新特性:类型注解
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #微信小程序:微信小程序常见的配置传旨
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (k8s中)docker netty OOM问题记录
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (笔试题)合法字符串
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (未解决)macOS matplotlib 中文是方框
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • . Flume面试题
  • .net core控制台应用程序初识
  • .net 反编译_.net反编译的相关问题
  • .net 生成二级域名
  • .net 无限分类
  • .ui文件相关
  • @Resource和@Autowired的区别
  • [20190416]完善shared latch测试脚本2.txt
  • [BIZ] - 1.金融交易系统特点
  • [C#]C# OpenVINO部署yolov8图像分类模型
  • [C#7] 1.Tuples(元组)
  • [CareerCup] 6.1 Find Heavy Bottle 寻找重瓶子
  • [CCIE历程]CCIE # 20604