Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Relsola
2023-12-27 19:16:46 +08:00
committed by GitHub
3 changed files with 98 additions and 47 deletions

View File

@ -218,22 +218,40 @@ class Solution:
### Go ### Go
```Go ```Go
// 思路: 使用栈来进行括号的匹配
// 时间复杂度 O(n)
// 空间复杂度 O(n)
func isValid(s string) bool { func isValid(s string) bool {
hash := map[byte]byte{')':'(', ']':'[', '}':'{'} // 使用切片模拟栈的行为
stack := make([]byte, 0) stack := make([]rune, 0)
if s == "" {
return true
}
for i := 0; i < len(s); i++ { // m 用于记录某个右括号对应的左括号
if s[i] == '(' || s[i] == '[' || s[i] == '{' { m := make(map[rune]rune)
stack = append(stack, s[i]) m[')'] = '('
} else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] { m[']'] = '['
stack = stack[:len(stack)-1] m['}'] = '{'
// 遍历字符串中的 rune
for _, c := range s {
// 左括号直接入栈
if c == '(' || c == '[' || c == '{' {
stack = append(stack, c)
} else { } else {
// 如果是右括号,先判断栈内是否还有元素
if len(stack) == 0 {
return false return false
} }
// 再判断栈顶元素是否能够匹配
peek := stack[len(stack)-1]
if peek != m[c] {
return false
} }
// 模拟栈顶弹出
stack = stack[:len(stack)-1]
}
}
// 若栈中不再包含元素,则能完全匹配
return len(stack) == 0 return len(stack) == 0
} }
``` ```

View File

@ -235,52 +235,77 @@ class MyQueue:
### Go ### Go
```Go ```Go
type MyQueue struct { // 通过切片实现一个栈
stackIn []int //输入栈 // 由于只是辅助实现算法题目,因此不做异常情况处理
stackOut []int //输出栈 type MyStack []int
func (s *MyStack) Push(v int) {
*s = append(*s, v)
} }
func (s *MyStack) Pop() int {
val := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return val
}
func (s *MyStack) Peek() int {
return (*s)[len(*s)-1]
}
func (s *MyStack) Size() int {
return len(*s)
}
func (s *MyStack) Empty() bool {
return s.Size() == 0
}
// ---------- 分界线 ----------
type MyQueue struct {
stackIn *MyStack
stackOut *MyStack
}
func Constructor() MyQueue { func Constructor() MyQueue {
return MyQueue{ return MyQueue {
stackIn: make([]int, 0), stackIn: &MyStack{},
stackOut: make([]int, 0), stackOut: &MyStack{},
} }
} }
// 往输入栈做push
func (this *MyQueue) Push(x int) { func (this *MyQueue) Push(x int) {
this.stackIn = append(this.stackIn, x) this.stackIn.Push(x)
} }
// 在输出栈做poppop时如果输出栈数据为空需要将输入栈全部数据导入如果非空则可直接使用
func (this *MyQueue) Pop() int { func (this *MyQueue) Pop() int {
inLen, outLen := len(this.stackIn), len(this.stackOut) this.fillStackOut()
if outLen == 0 { return this.stackOut.Pop()
if inLen == 0 {
return -1
}
for i := inLen - 1; i >= 0; i-- {
this.stackOut = append(this.stackOut, this.stackIn[i])
}
this.stackIn = []int{} //导出后清空
outLen = len(this.stackOut) //更新长度值
}
val := this.stackOut[outLen-1]
this.stackOut = this.stackOut[:outLen-1]
return val
} }
func (this *MyQueue) Peek() int { func (this *MyQueue) Peek() int {
val := this.Pop() this.fillStackOut()
if val == -1 { return this.stackOut.Peek()
return -1
}
this.stackOut = append(this.stackOut, val)
return val
} }
func (this *MyQueue) Empty() bool { func (this *MyQueue) Empty() bool {
return len(this.stackIn) == 0 && len(this.stackOut) == 0 return this.stackIn.Empty() && this.stackOut.Empty()
}
// fillStackOut 填充输出栈
func (this *MyQueue) fillStackOut() {
if this.stackOut.Empty() {
for !this.stackIn.Empty() {
val := this.stackIn.Pop()
this.stackOut.Push(val)
}
}
} }
``` ```

View File

@ -235,7 +235,15 @@ func main(){
### python ### python
```Python
class Solution:
def change(self, s):
lst = list(s) # Python里面的string也是不可改的所以也是需要额外空间的。空间复杂度O(n)。
for i in range(len(lst)):
if lst[i].isdigit():
lst[i] = "number"
return ''.join(lst)
```
### JavaScript: ### JavaScript: