栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

LeetCode 284 窥探迭代器[迭代器 构造类] HERODING的LeetCode之路

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode 284 窥探迭代器[迭代器 构造类] HERODING的LeetCode之路


解题思路:
本质是继承 Iterator 类用来实现一个新的 Iterator 只不过多了一个peek,那么就很好解决了,next() 函数和 hasNext() 函数直接返回原来的 Iterator 的函数, peek() 函数用一个指针指向当前的位置,然后返回该指针的next即可,代码如下:


class PeekingIterator : public Iterator {
public:
	PeekingIterator(const vector& nums) : Iterator(nums) {
	    // Initialize any member here.
	    // **DO NOT** save a copy of nums and manipulate it directly.
	    // You should only use the Iterator interface methods.
	    
	}
	
    // Returns the next element in the iteration without advancing the iterator.
	int peek() {
        auto temp = *this;
        return temp.next();
	}
	
	// hasNext() and next() should behave the same as in the Iterator interface.
	// Override them if needed.
	int next() {
	    return Iterator :: next();
	}
	
	bool hasNext() const {
	    return Iterator :: hasNext();
	}
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297231.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号