From 5d56461d39759a10feb0c2c155eee70cdfbaa958 Mon Sep 17 00:00:00 2001 From: fifi1120 Date: Thu, 21 Dec 2023 14:22:44 -0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Kama54.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E7=9A=84Python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kama54.替换数字.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/problems/kama54.替换数字.md b/problems/kama54.替换数字.md index 0f8daa21..2b3d53de 100644 --- a/problems/kama54.替换数字.md +++ b/problems/kama54.替换数字.md @@ -235,7 +235,15 @@ func main(){ ### 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: From 1fbe05532511a96e48b118172ed5f9e2f21ad7c6 Mon Sep 17 00:00:00 2001 From: cherrypicker Date: Fri, 22 Dec 2023 10:02:23 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update0232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97=20=E4=BC=98=E5=8C=96Go=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用切片模拟栈的实现,即 MyStack --- problems/0232.用栈实现队列.md | 87 +++++++++++++++++++---------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 24374010..41933ca4 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -235,52 +235,77 @@ class MyQueue: ### 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 { - return MyQueue{ - stackIn: make([]int, 0), - stackOut: make([]int, 0), + return MyQueue { + stackIn: &MyStack{}, + stackOut: &MyStack{}, } } -// 往输入栈做push -func (this *MyQueue) Push(x int) { - this.stackIn = append(this.stackIn, x) + +func (this *MyQueue) Push(x int) { + this.stackIn.Push(x) } -// 在输出栈做pop,pop时如果输出栈数据为空,需要将输入栈全部数据导入,如果非空,则可直接使用 + func (this *MyQueue) Pop() int { - inLen, outLen := len(this.stackIn), len(this.stackOut) - if outLen == 0 { - 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 + this.fillStackOut() + return this.stackOut.Pop() } + func (this *MyQueue) Peek() int { - val := this.Pop() - if val == -1 { - return -1 - } - this.stackOut = append(this.stackOut, val) - return val + this.fillStackOut() + return this.stackOut.Peek() } + 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) + } + } } ``` From 9319ce9cdcab03027644ce1cff57b4060d53bc99 Mon Sep 17 00:00:00 2001 From: cherrypicker Date: Sat, 23 Dec 2023 08:34:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update0020.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7=20Go=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原有的实现已经足够简洁清晰,这个版本的代码增加了注释,提升了可读性。 --- problems/0020.有效的括号.md | 48 ++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 045c79ee..17fbe2be 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -218,23 +218,41 @@ class Solution: ### Go: ```Go +// 思路: 使用栈来进行括号的匹配 +// 时间复杂度 O(n) +// 空间复杂度 O(n) func isValid(s string) bool { - hash := map[byte]byte{')':'(', ']':'[', '}':'{'} - stack := make([]byte, 0) - if s == "" { - return true - } + // 使用切片模拟栈的行为 + stack := make([]rune, 0) - for i := 0; i < len(s); i++ { - if s[i] == '(' || s[i] == '[' || s[i] == '{' { - stack = append(stack, s[i]) - } else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] { - stack = stack[:len(stack)-1] - } else { - return false - } - } - return len(stack) == 0 + // m 用于记录某个右括号对应的左括号 + m := make(map[rune]rune) + m[')'] = '(' + m[']'] = '[' + m['}'] = '{' + + // 遍历字符串中的 rune + for _, c := range s { + // 左括号直接入栈 + if c == '(' || c == '[' || c == '{' { + stack = append(stack, c) + } else { + // 如果是右括号,先判断栈内是否还有元素 + if len(stack) == 0 { + return false + } + // 再判断栈顶元素是否能够匹配 + peek := stack[len(stack)-1] + if peek != m[c] { + return false + } + // 模拟栈顶弹出 + stack = stack[:len(stack)-1] + } + } + + // 若栈中不再包含元素,则能完全匹配 + return len(stack) == 0 } ```