Skip to content

Commit

Permalink
Merge pull request #643 from 0xff-dev/284
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 284
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 253494c + 1efd774 commit 2c1f170
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 27 deletions.
38 changes: 22 additions & 16 deletions leetcode/201-300/0284.Peeking-Iterator/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# [284.Peeking Iterator][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.

**Example 1:**
Implement the `PeekingIterator` class:

```
Input: a = "11", b = "1"
Output: "100"
```
- `PeekingIterator(Iterator<int> nums)` Initializes the object with the given integer iterator `iterator`.
- `int next()` Returns the next element in the array and moves the pointer to the next element.
- `boolean hasNext()` Returns `true` if there are still elements in the array.
- `int peek()` Returns the next element in the array without moving the pointer.

## 题意
> ...
**Note**: Each language may have a different implementation of the constructor and `Iterator`, but they all support the `int next()` and `boolean hasNext()` functions.

## 题解
**Example 1:**

### 思路1
> ...
Peeking Iterator
```go
```

Input
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
Output
[null, 1, 2, 2, 3, false]
Explanation
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]
peekingIterator.next(); // return 1, the pointer moves to the next element [1,2,3].
peekingIterator.peek(); // return 2, the pointer does not move [1,2,3].
peekingIterator.next(); // return 2, the pointer moves to the next element [1,2,3]
peekingIterator.next(); // return 3, the pointer moves to the next element [1,2,3]
peekingIterator.hasNext(); // return False
```

## 结语

Expand Down
63 changes: 61 additions & 2 deletions leetcode/201-300/0284.Peeking-Iterator/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
package Solution

func Solution(x bool) bool {
return x
type Iterator struct {
data []int
idx int
}

func (this *Iterator) hasNext() bool {
return this.idx < len(this.data)-1
}
func (this *Iterator) next() int {
ans := this.data[this.idx]
this.idx++
return ans
}

type PeekingIterator struct {
iter *Iterator
peekV int
}

func Constructor284(iter *Iterator) *PeekingIterator {
return &PeekingIterator{iter: iter, peekV: -1}
}

func (this *PeekingIterator) hasNext() bool {
if !this.iter.hasNext() {
return this.peekV != -1
}
return this.iter.hasNext()
}

func (this *PeekingIterator) next() int {
if this.peekV != -1 {
ans := this.peekV
this.peekV = -1
return ans
}
return this.iter.next()
}

func (this *PeekingIterator) peek() int {
if this.peekV == -1 {
this.peekV = this.iter.next()
}
return this.peekV
}

func Solution(iter *Iterator, options []string) []interface{} {
ans := make([]interface{}, 0)
o := Constructor284(iter)
for _, op := range options {
if op == "next" {
ans = append(ans, o.next())
continue
}
if op == "peek" {
ans = append(ans, o.peek())
continue
}
ans = append(ans, o.hasNext())
}
return ans
}
17 changes: 8 additions & 9 deletions leetcode/201-300/0284.Peeking-Iterator/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs *Iterator
operations []string
expect []interface{}
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &Iterator{data: []int{1, 2, 3}}, []string{"next", "peek", "next", "next", "hasNext"}, []interface{}{1, 2, 2, 3, false}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.operations)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 2c1f170

Please sign in to comment.