diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index d8945eff..a3f78c39 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -336,8 +336,26 @@ class Solution { ``` Python: - - +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +//递归法 +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + res = [] //把二叉搜索树按中序遍历写成list + def buildalist(root): + if not root: return + buildalist(root.left) //左 + res.append(root.val) //中 + buildalist(root.right) //右 + return res + buildalist(root) + return res == sorted(res) and len(set(res)) == len(res) //检查list里的数有没有重复元素,以及是否按从小到大排列 +``` Go: ```Go import "math" @@ -365,4 +383,4 @@ func isBST(root *TreeNode, min, max int) bool { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 4018364a..8dff2b86 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -156,6 +156,7 @@ public: Java: +使用两个 Queue 实现 ```java class MyStack { @@ -205,7 +206,94 @@ class MyStack { * boolean param_4 = obj.empty(); */ ``` +使用两个 Deque 实现 +```java +class MyStack { + // Deque 接口继承了 Queue 接口 + // 所以 Queue 中的 add、poll、peek等效于 Deque 中的 addLast、pollFirst、peekFirst + Deque que1; // 和栈中保持一样元素的队列 + Deque que2; // 辅助队列 + /** Initialize your data structure here. */ + public MyStack() { + que1 = new ArrayDeque<>(); + que2 = new ArrayDeque<>(); + } + + /** Push element x onto stack. */ + public void push(int x) { + que1.addLast(x); + } + + /** Removes the element on top of the stack and returns that element. */ + public int pop() { + int size = que1.size(); + size--; + // 将 que1 导入 que2 ,但留下最后一个值 + while (size-- > 0) { + que2.addLast(que1.peekFirst()); + que1.pollFirst(); + } + int res = que1.pollFirst(); + // 将 que2 对象的引用赋给了 que1 ,此时 que1,que2 指向同一个队列 + que1 = que2; + // 如果直接操作 que2,que1 也会受到影响,所以为 que2 分配一个新的空间 + que2 = new ArrayDeque<>(); + return res; + } + + /** Get the top element. */ + public int top() { + return que1.peekLast(); + } + + /** Returns whether the stack is empty. */ + public boolean empty() { + return que1.isEmpty(); + } +} +``` +优化,使用一个 Deque 实现 +```java +class MyStack { + // Deque 接口继承了 Queue 接口 + // 所以 Queue 中的 add、poll、peek等效于 Deque 中的 addLast、pollFirst、peekFirst + Deque que1; + /** Initialize your data structure here. */ + public MyStack() { + que1 = new ArrayDeque<>(); + } + + /** Push element x onto stack. */ + public void push(int x) { + que1.addLast(x); + } + + /** Removes the element on top of the stack and returns that element. */ + public int pop() { + int size = que1.size(); + size--; + // 将 que1 导入 que2 ,但留下最后一个值 + while (size-- > 0) { + que1.addLast(que1.peekFirst()); + que1.pollFirst(); + } + + int res = que1.pollFirst(); + return res; + } + + /** Get the top element. */ + public int top() { + return que1.peekLast(); + } + + /** Returns whether the stack is empty. */ + public boolean empty() { + return que1.isEmpty(); + } +} +``` Python: @@ -276,4 +364,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 3cf058d7..ba057982 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -285,6 +285,73 @@ Python: Go: +```Go +type MyQueue struct { + stack []int + back []int +} + +/** Initialize your data structure here. */ +func Constructor() MyQueue { + return MyQueue{ + stack: make([]int, 0), + back: make([]int, 0), + } +} + +/** Push element x to the back of queue. */ +func (this *MyQueue) Push(x int) { + for len(this.back) != 0 { + val := this.back[len(this.back)-1] + this.back = this.back[:len(this.back)-1] + this.stack = append(this.stack, val) + } + this.stack = append(this.stack, x) +} + +/** Removes the element from in front of queue and returns that element. */ +func (this *MyQueue) Pop() int { + for len(this.stack) != 0 { + val := this.stack[len(this.stack)-1] + this.stack = this.stack[:len(this.stack)-1] + this.back = append(this.back, val) + } + if len(this.back) == 0 { + return 0 + } + val := this.back[len(this.back)-1] + this.back = this.back[:len(this.back)-1] + return val +} + +/** Get the front element. */ +func (this *MyQueue) Peek() int { + for len(this.stack) != 0 { + val := this.stack[len(this.stack)-1] + this.stack = this.stack[:len(this.stack)-1] + this.back = append(this.back, val) + } + if len(this.back) == 0 { + return 0 + } + val := this.back[len(this.back)-1] + return val +} + +/** Returns whether the queue is empty. */ +func (this *MyQueue) Empty() bool { + return len(this.stack) == 0 && len(this.back) == 0 +} + +/** + * Your MyQueue object will be instantiated and called as such: + * obj := Constructor(); + * obj.Push(x); + * param_2 := obj.Pop(); + * param_3 := obj.Peek(); + * param_4 := obj.Empty(); + */ + ``` diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index d7acef99..d2253a4e 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -173,7 +173,35 @@ Python: Go: - +```Go +func longestPalindromeSubseq(s string) int { + str:=[]byte(s) + dp:=make([][]int,len(s)) + for i:=0;i=0;j--{ + if str[i]==str[j]{ + if j==i-1{ + dp[j][i]=2 + }else{ + dp[j][i]=dp[j+1][i-1]+2 + } + }else{ + dp[j][i]=Max(dp[j+1][i],dp[j][i-1]) + } + } + } + return dp[0][len(s)-1] +} +func Max(a,b int)int{ + if a>b{ + return a + } + return b +} +``` diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 848454de..ce37a953 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -312,7 +312,23 @@ class Solution { ``` Python: - +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +//递归法*前序遍历 +class Solution: + def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode: + if not root1: return root2 // 如果t1为空,合并之后就应该是t2 + if not root2: return root1 // 如果t2为空,合并之后就应该是t1 + root1.val = root1.val + root2.val //中 + root1.left = self.mergeTrees(root1.left , root2.left) //左 + root1.right = self.mergeTrees(root1.right , root2.right) //右 + return root1 //root1修改了结构和数值 +``` Go: @@ -323,4 +339,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 277ef681..3e35fbfb 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -212,13 +212,18 @@ Python: 递归法: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: - if root is None: - return None - if val < root.val: return self.searchBST(root.left, val) - elif val > root.val: return self.searchBST(root.right, val) - else: return root + if not root or root.val == val: return root //为空或者已经找到都是直接返回root,所以合并了 + if root.val > val: return self.searchBST(root.left,val) //注意一定要加return + else: return self.searchBST(root.right,val) + ``` 迭代法: @@ -243,4 +248,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +