Skip to content

Commit 6dedf42

Browse files
Merge pull request #2054 from CodingTestStudy2/변지협
[변지협] Day19
2 parents eab3b3d + 0c2b4eb commit 6dedf42

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'''
2+
1. 아이디어:
3+
그냥 문제에 주어진대로 구현하면 됨.
4+
2. 시간복잡도:
5+
o(n)
6+
3. 알고리즘:
7+
'''
8+
9+
# Below is the interface for Iterator, which is already defined for you.
10+
#
11+
# class Iterator:
12+
# def __init__(self, nums):
13+
# """
14+
# Initializes an iterator object to the beginning of a list.
15+
# :type nums: List[int]
16+
# """
17+
#
18+
# def hasNext(self):
19+
# """
20+
# Returns true if the iteration has more elements.
21+
# :rtype: bool
22+
# """
23+
#
24+
# def next(self):
25+
# """
26+
# Returns the next element in the iteration.
27+
# :rtype: int
28+
# """
29+
30+
class PeekingIterator:
31+
it = []
32+
i = 0
33+
34+
def __init__(self, iterator):
35+
"""
36+
Initialize your data structure here.
37+
:type iterator: Iterator
38+
"""
39+
tmp = []
40+
while iterator.hasNext():
41+
tmp.append(iterator.next())
42+
self.it = tmp
43+
44+
45+
def peek(self):
46+
"""
47+
Returns the next element in the iteration without advancing the iterator.
48+
:rtype: int
49+
"""
50+
return self.it[self.i]
51+
52+
def next(self):
53+
"""
54+
:rtype: int
55+
"""
56+
tmp = self.it[self.i]
57+
self.i +=1
58+
return tmp
59+
60+
61+
def hasNext(self):
62+
"""
63+
:rtype: bool
64+
"""
65+
try:
66+
tmp = self.it[self.i]
67+
return True
68+
except:
69+
return False
70+
71+
# Your PeekingIterator object will be instantiated and called as such:
72+
# iter = PeekingIterator(Iterator(nums))
73+
# while iter.hasNext():
74+
# val = iter.peek() # Get the next element but not advance the iterator.
75+
# iter.next() # Should return the same value as [val].

0 commit comments

Comments
 (0)