-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #643 from 0xff-dev/284
Add solution and test-cases for problem 284
- Loading branch information
Showing
3 changed files
with
91 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters