Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions leetcode3/변지협/v3/284. Peeking Iterator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'''
1. 아이디어:
그냥 문제에 주어진대로 구현하면 됨.
2. 시간복잡도:
o(n)
3. 알고리즘:
'''

# Below is the interface for Iterator, which is already defined for you.
#
# class Iterator:
# 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
# """

class PeekingIterator:
it = []
i = 0

def __init__(self, iterator):
"""
Initialize your data structure here.
:type iterator: Iterator
"""
tmp = []
while iterator.hasNext():
tmp.append(iterator.next())
self.it = tmp


def peek(self):
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
return self.it[self.i]

def next(self):
"""
:rtype: int
"""
tmp = self.it[self.i]
self.i +=1
return tmp


def hasNext(self):
"""
:rtype: bool
"""
try:
tmp = self.it[self.i]
return True
except:
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].
Loading