mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -336,8 +336,26 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
||||||
```Go
|
```Go
|
||||||
import "math"
|
import "math"
|
||||||
@ -365,4 +383,4 @@ func isBST(root *TreeNode, min, max int) bool {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -156,6 +156,7 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
使用两个 Queue 实现
|
||||||
```java
|
```java
|
||||||
class MyStack {
|
class MyStack {
|
||||||
|
|
||||||
@ -205,7 +206,94 @@ class MyStack {
|
|||||||
* boolean param_4 = obj.empty();
|
* boolean param_4 = obj.empty();
|
||||||
*/
|
*/
|
||||||
```
|
```
|
||||||
|
使用两个 Deque 实现
|
||||||
|
```java
|
||||||
|
class MyStack {
|
||||||
|
// Deque 接口继承了 Queue 接口
|
||||||
|
// 所以 Queue 中的 add、poll、peek等效于 Deque 中的 addLast、pollFirst、peekFirst
|
||||||
|
Deque<Integer> que1; // 和栈中保持一样元素的队列
|
||||||
|
Deque<Integer> 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<Integer> 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:
|
Python:
|
||||||
|
|
||||||
@ -276,4 +364,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -285,6 +285,73 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
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();
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -173,7 +173,35 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func longestPalindromeSubseq(s string) int {
|
||||||
|
str:=[]byte(s)
|
||||||
|
dp:=make([][]int,len(s))
|
||||||
|
for i:=0;i<len(s);i++{
|
||||||
|
dp[i]=make([]int,len(s))
|
||||||
|
}
|
||||||
|
for i:=1;i<len(s);i++{
|
||||||
|
for j:=i-1;j>=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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -312,7 +312,23 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
Go:
|
||||||
|
|
||||||
@ -323,4 +339,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -212,13 +212,18 @@ Python:
|
|||||||
递归法:
|
递归法:
|
||||||
|
|
||||||
```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:
|
class Solution:
|
||||||
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
|
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
|
||||||
if root is None:
|
if not root or root.val == val: return root //为空或者已经找到都是直接返回root,所以合并了
|
||||||
return None
|
if root.val > val: return self.searchBST(root.left,val) //注意一定要加return
|
||||||
if val < root.val: return self.searchBST(root.left, val)
|
else: return self.searchBST(root.right,val)
|
||||||
elif val > root.val: return self.searchBST(root.right, val)
|
|
||||||
else: return root
|
|
||||||
```
|
```
|
||||||
|
|
||||||
迭代法:
|
迭代法:
|
||||||
@ -243,4 +248,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user