go: add «1381. Design a Stack With Increment Operation»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
7116e58b36
commit
3033003e6c
1 changed files with 39 additions and 0 deletions
39
go/design-a-stack-with-increment-operation.go
Normal file
39
go/design-a-stack-with-increment-operation.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
type CustomStack struct {
|
||||
maxSize int
|
||||
stack []int
|
||||
size int
|
||||
}
|
||||
|
||||
func Constructor(maxSize int) CustomStack {
|
||||
return CustomStack{
|
||||
maxSize: maxSize,
|
||||
stack: make([]int, maxSize),
|
||||
size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *CustomStack) Push(x int) {
|
||||
if this.size >= this.maxSize {
|
||||
return
|
||||
}
|
||||
|
||||
this.stack[this.size] = x
|
||||
this.size++
|
||||
}
|
||||
|
||||
func (this *CustomStack) Pop() int {
|
||||
if this.size <= 0 {
|
||||
return -1
|
||||
}
|
||||
|
||||
this.size--
|
||||
return this.stack[this.size]
|
||||
}
|
||||
|
||||
func (this *CustomStack) Increment(k int, val int) {
|
||||
for i := 0; i < k && i < this.size; i++ {
|
||||
this.stack[i] += val
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue