284. Peeking Iterator

  • 35.2%

https://leetcode.com/problems/peeking-iterator/#/description

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be returned by the next call to next().

Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3].

Call next() gets you 1, the first element in the list.

Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.

You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false.

Hint:

  1. Think of “looking ahead”. You want to cache the next element.
  2. Is one variable sufficient? Why or why not?
  3. Test your design with call order of peek() before next() vs next() before peek().
  4. For a clean implementation, check out Google’s guava library source code.

Follow up: How would you extend your design to be generic and work with all types, not just integer?


3ms, 3.96%, October 15, 2016

https://discuss.leetcode.com/topic/24909/simple-c-solution-1-line-per-method-without-extra-member-variables

Simple C++ solution (1 line per method) without extra member variables

Since Iterator has a copy constructor, we can just use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class PeekingIterator : public Iterator
{
public:
PeekingIterator(const vector<int> &nums) : Iterator(nums)
{
}

int peek()
{
return Iterator(*this).next();
}

int next()
{
return Iterator::next();
}

bool hasNext() const
{
return Iterator::hasNext();
}
};

https://discuss.leetcode.com/topic/25875/another-c-solution-with-one-line-in-peek-and-hasnext-ac

Another C++ solution with one line in peek() and hasNext(), AC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class PeekingIterator : public Iterator {
private:
int m_next;
bool m_hasnext;
public:
PeekingIterator(const vector<int>& nums) : Iterator(nums) {
m_hasnext = Iterator::hasNext();
if (m_hasnext) m_next = Iterator::next();
}

int peek() {
return m_next;
}

int next() {
int t = m_next;
m_hasnext = Iterator::hasNext();
if (m_hasnext) m_next = Iterator::next();
return t;
}

bool hasNext() const {
return m_hasnext;
}
};

https://discuss.leetcode.com/topic/26190/my-4ms-c-supper-easy-solution

My 4ms c++ supper easy solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class PeekingIterator : public Iterator {
public:
PeekingIterator(const vector<int>& 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() {
if(hasNext()){
Iterator it(*this);
return it.next();
}
}

// hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
int next() {
Iterator::next();
}

bool hasNext() const {
Iterator::hasNext();
}
};

https://discuss.leetcode.com/topic/24857/10-line-c-and-14-line-java-implementation

10-line C++ and 14-line Java Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class PeekingIterator : public Iterator {
bool hasPeeked;
int peekedElem;
public:
PeekingIterator(const vector<int>& num) : Iterator(num) {
hasPeeked = false;
}

int peek() {
peekedElem = hasPeeked?peekedElem:Iterator::next();
hasPeeked = true;
return peekedElem;
}

int next() {
int nextElem = hasPeeked?peekedElem:Iterator::next();
hasPeeked = false;
return nextElem;
}

bool hasNext() const {
return hasPeeked||Iterator::hasNext();
}
};

Java implementation, inspired by Google’s guava library source code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class PeekingIterator implements Iterator<Integer> {
private final Iterator<Integer> iterator;
private boolean hasPeeked;
private Integer peekedElement;

public PeekingIterator(Iterator<Integer> iterator) {
if(iterator==null)
throw new NullPointerException();
else
this.iterator = iterator;
}

public Integer peek() {
peekedElement = hasPeeked?peekedElement:iterator.next();
hasPeeked = true;
return peekedElement;
}

@Override
public Integer next() {
int nextElem = hasPeeked?peekedElement:iterator.next();
hasPeeked = false;
return nextElem;
}

@Override
public boolean hasNext() {
return hasPeeked || iterator.hasNext();
}
}

https://discuss.leetcode.com/topic/25308/simple-python-solution

Simple Python Solution

Store the next value outside the iterator. When next is called return the stored value and populate with next value from iterator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class PeekingIterator(object):
def __init__(self, iterator):
self.iter = iterator
self.temp = self.iter.next() if self.iter.hasNext() else None

def peek(self):
return self.temp

def next(self):
ret = self.temp
self.temp = self.iter.next() if self.iter.hasNext() else None
return ret

def hasNext(self):
return self.temp is not None

111ms, 45.64%, October 15, 2016

https://discuss.leetcode.com/topic/24883/concise-java-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
private Integer next = null;
private Iterator<Integer> iter;

public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
iter = iterator;
if(iter.hasNext())
next = iter.next();
}

// Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
return next;
}

// hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
Integer res = next;
next = iter.hasNext() ? iter.next() : null;
return res;
}

@Override
public boolean hasNext() {
return next != null;
}
}
谢谢你,可爱的朋友。