mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 structures
This commit is contained in:
33
structures/Stack.go
Normal file
33
structures/Stack.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package structures
|
||||
|
||||
// Stack 是用于存放 int 的 栈
|
||||
type Stack struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// NewStack 返回 *kit.Stack
|
||||
func NewStack() *Stack {
|
||||
return &Stack{nums: []int{}}
|
||||
}
|
||||
|
||||
// Push 把 n 放入 栈
|
||||
func (s *Stack) Push(n int) {
|
||||
s.nums = append(s.nums, n)
|
||||
}
|
||||
|
||||
// Pop 从 s 中取出最后放入 栈 的值
|
||||
func (s *Stack) Pop() int {
|
||||
res := s.nums[len(s.nums)-1]
|
||||
s.nums = s.nums[:len(s.nums)-1]
|
||||
return res
|
||||
}
|
||||
|
||||
// Len 返回 s 的长度
|
||||
func (s *Stack) Len() int {
|
||||
return len(s.nums)
|
||||
}
|
||||
|
||||
// IsEmpty 反馈 s 是否为空
|
||||
func (s *Stack) IsEmpty() bool {
|
||||
return s.Len() == 0
|
||||
}
|
||||
Reference in New Issue
Block a user