# Below is the interface for Iterator, which is already defined for you.
#
# class Iterator(object):
# def __init__(self, nums):
# """
# Initializes an iterator object to the beginning of a list.
# :type nums: List[int]
# """
#
# def hasNext(self):
# """
# Returns true if the iteration has more elements.
# :rtype: bool
# """
#
# def next(self):
# """
# Returns the next element in the iteration.
# :rtype: int
# """
#the issue is that when we peek we'll call Iterator().next() as well as when we actually want to get the next elements
#therefore, in order to avoid calling Iterator().next() twice
#we use a boolean variable to flag if the next element has been looked at (the position of the pointer)
#and when we have peeked but havent' asked for the element, we'll simply return the peeked element.
class PeekingIterator(object):
def __init__(self, iterator):
"""
Initialize your data structure here.
:type iterator: Iterator
"""
#default flag=False, indicate that the next elements hasn't been looked at
self.flag=False
self.iterator=iterator
def peek(self):
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
#if the next element hasn't been looked at, store the next value in self.value, then set flag to true
if (not self.flag):
self.value=self.iterator.next()
self.flag=True
return self.value
def next(self):
"""
:rtype: int
"""
if (not self.flag):
self.value=self.iterator.next()
self.flag=False
return self.value
def hasNext(self):
"""
:rtype: bool
"""
if(self.flag):return True
if(self.iterator.hasNext()):return True
return False
# Your PeekingIterator object will be instantiated and called as such:
# iter = PeekingIterator(Iterator(nums))
# while iter.hasNext():
# val = iter.peek() # Get the next element but not advance the iterator.
# iter.next() # Should return the same value as [val].
284. Peeking Iterator
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)瓦堵,“玉大人基协,你說(shuō)我怎么就攤上這事」接茫” “怎么了澜驮?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)惋鸥。 經(jīng)常有香客問(wèn)我杂穷,道長(zhǎng),這世上最難降的妖魔是什么卦绣? 我笑而不...
- 正文 為了忘掉前任耐量,我火速辦了婚禮,結(jié)果婚禮上滤港,老公的妹妹穿的比我還像新娘廊蜒。我一直安慰自己,他們只是感情好溅漾,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布山叮。 她就那樣靜靜地躺著,像睡著了一般添履。 火紅的嫁衣襯著肌膚如雪屁倔。 梳的紋絲不亂的頭發(fā)上,一...
- 那天缝龄,我揣著相機(jī)與錄音汰现,去河邊找鬼挂谍。 笑死,一個(gè)胖子當(dāng)著我的面吹牛瞎饲,可吹牛的內(nèi)容都是我干的口叙。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼嗅战,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼妄田!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起驮捍,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤疟呐,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后东且,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體启具,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年珊泳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鲁冯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站验毡,受9級(jí)特大地震影響衡创,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜米罚,卻給世界環(huán)境...
- 文/蒙蒙 一钧汹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸癣籽。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至动看,卻和暖如春尊剔,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背菱皆。 一陣腳步聲響...
- 正文 我出身青樓京痢,卻偏偏與公主長(zhǎng)得像奶甘,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子祭椰,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- Given an Iterator class interface with methods: next() an...
- Question Given an Iterator class interface with methods: ...
- Given an Iterator class interface with methods: next() an...
- "react-native": "0.46.1"這個(gè)問(wèn)題產(chǎn)生原因: /Users/Vanessa/.rncache...
- 06迭代器的概述 A:迭代器概述:a:java中提供了很多個(gè)集合臭家,它們?cè)诖鎯?chǔ)元素時(shí),采用的存儲(chǔ)方式不同方淤。我們要取出...