mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -147,6 +147,7 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
def climbStairs(self, n: int) -> int:
|
def climbStairs(self, n: int) -> int:
|
||||||
@ -163,8 +164,6 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
func climbStairs(n int) int {
|
func climbStairs(n int) int {
|
||||||
|
@ -252,6 +252,23 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
|
||||||
|
'''排列'''
|
||||||
|
dp = [False]*(len(s) + 1)
|
||||||
|
dp[0] = True
|
||||||
|
# 遍历背包
|
||||||
|
for j in range(1, len(s) + 1):
|
||||||
|
# 遍历单词
|
||||||
|
for word in wordDict:
|
||||||
|
if j >= len(word):
|
||||||
|
dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j])
|
||||||
|
return dp[len(s)]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
输入:n = 13
|
输入:n = 13
|
||||||
输出:2
|
输出:2
|
||||||
解释:13 = 4 + 9
|
解释:13 = 4 + 9
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
* 1 <= n <= 10^4
|
* 1 <= n <= 10^4
|
||||||
|
|
||||||
@ -184,6 +184,38 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def numSquares(self, n: int) -> int:
|
||||||
|
'''版本一'''
|
||||||
|
# 初始化
|
||||||
|
nums = [i**2 for i in range(1, n + 1) if i**2 <= n]
|
||||||
|
dp = [10**4]*(n + 1)
|
||||||
|
dp[0] = 0
|
||||||
|
# 遍历背包
|
||||||
|
for j in range(1, n + 1):
|
||||||
|
# 遍历物品
|
||||||
|
for num in nums:
|
||||||
|
if j >= num:
|
||||||
|
dp[j] = min(dp[j], dp[j - num] + 1)
|
||||||
|
return dp[n]
|
||||||
|
|
||||||
|
def numSquares1(self, n: int) -> int:
|
||||||
|
'''版本二'''
|
||||||
|
# 初始化
|
||||||
|
nums = [i**2 for i in range(1, n + 1) if i**2 <= n]
|
||||||
|
dp = [10**4]*(n + 1)
|
||||||
|
dp[0] = 0
|
||||||
|
# 遍历物品
|
||||||
|
for num in nums:
|
||||||
|
# 遍历背包
|
||||||
|
for j in range(num, n + 1)
|
||||||
|
dp[j] = min(dp[j], dp[j - num] + 1)
|
||||||
|
return dp[n]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
示例 5:
|
示例 5:
|
||||||
输入:coins = [1], amount = 2
|
输入:coins = [1], amount = 2
|
||||||
输出:2
|
输出:2
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
|
|
||||||
* 1 <= coins.length <= 12
|
* 1 <= coins.length <= 12
|
||||||
@ -209,6 +209,36 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||||
|
'''版本一'''
|
||||||
|
# 初始化
|
||||||
|
dp = [amount + 1]*(amount + 1)
|
||||||
|
dp[0] = 0
|
||||||
|
# 遍历物品
|
||||||
|
for coin in coins:
|
||||||
|
# 遍历背包
|
||||||
|
for j in range(coin, amount + 1):
|
||||||
|
dp[j] = min(dp[j], dp[j - coin] + 1)
|
||||||
|
return dp[amount] if dp[amount] < amount + 1 else -1
|
||||||
|
|
||||||
|
def coinChange1(self, coins: List[int], amount: int) -> int:
|
||||||
|
'''版本二'''
|
||||||
|
# 初始化
|
||||||
|
dp = [amount + 1]*(amount + 1)
|
||||||
|
dp[0] = 0
|
||||||
|
# 遍历物品
|
||||||
|
for j in range(1, amount + 1):
|
||||||
|
# 遍历背包
|
||||||
|
for coin in coins:
|
||||||
|
if j >= coin:
|
||||||
|
dp[j] = min(dp[j], dp[j - coin] + 1)
|
||||||
|
return dp[amount] if dp[amount] < amount + 1 else -1
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
|
@ -154,7 +154,25 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int findLength(int[] nums1, int[] nums2) {
|
||||||
|
int result = 0;
|
||||||
|
int[][] dp = new int[nums1.length + 1][nums2.length + 1];
|
||||||
|
|
||||||
|
for (int i = 1; i < nums1.length + 1; i++) {
|
||||||
|
for (int j = 1; j < nums2.length + 1; j++) {
|
||||||
|
if (nums1[i - 1] == nums2[j - 1]) {
|
||||||
|
dp[i][j] = dp[i - 1][j - 1] + 1;
|
||||||
|
max = Math.max(max, dp[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
@ -337,6 +337,110 @@ class Solution:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
100.相同的树
|
||||||
|
```go
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func isSameTree(p *TreeNode, q *TreeNode) bool {
|
||||||
|
switch {
|
||||||
|
case p == nil && q == nil:
|
||||||
|
return true
|
||||||
|
case p == nil || q == nil:
|
||||||
|
fallthrough
|
||||||
|
case p.Val != q.Val:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
257.二叉的所有路径
|
||||||
|
> 递归法
|
||||||
|
|
||||||
|
```go
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func binaryTreePaths(root *TreeNode) []string {
|
||||||
|
var result []string
|
||||||
|
traversal(root,&result,"")
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
func traversal(root *TreeNode,result *[]string,pathStr string){
|
||||||
|
//判断是否为第一个元素
|
||||||
|
if len(pathStr)!=0{
|
||||||
|
pathStr=pathStr+"->"+strconv.Itoa(root.Val)
|
||||||
|
}else{
|
||||||
|
pathStr=strconv.Itoa(root.Val)
|
||||||
|
}
|
||||||
|
//判断是否为叶子节点
|
||||||
|
if root.Left==nil&&root.Right==nil{
|
||||||
|
*result=append(*result,pathStr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//左右
|
||||||
|
if root.Left!=nil{
|
||||||
|
traversal(root.Left,result,pathStr)
|
||||||
|
}
|
||||||
|
if root.Right!=nil{
|
||||||
|
traversal(root.Right,result,pathStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 回溯法
|
||||||
|
|
||||||
|
```go
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func binaryTreePaths(root *TreeNode) []string {
|
||||||
|
var result []string
|
||||||
|
var path []int
|
||||||
|
traversal(root,&result,&path)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
func traversal(root *TreeNode,result *[]string,path *[]int){
|
||||||
|
*path=append(*path,root.Val)
|
||||||
|
//判断是否为叶子节点
|
||||||
|
if root.Left==nil&&root.Right==nil{
|
||||||
|
pathStr:=strconv.Itoa((*path)[0])
|
||||||
|
for i:=1;i<len(*path);i++{
|
||||||
|
pathStr=pathStr+"->"+strconv.Itoa((*path)[i])
|
||||||
|
}
|
||||||
|
*result=append(*result,pathStr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//左右
|
||||||
|
if root.Left!=nil{
|
||||||
|
traversal(root.Left,result,path)
|
||||||
|
*path=(*path)[:len(*path)-1]//回溯到上一个节点(因为traversal会加下一个节点值到path中)
|
||||||
|
}
|
||||||
|
if root.Right!=nil{
|
||||||
|
traversal(root.Right,result,path)
|
||||||
|
*path=(*path)[:len(*path)-1]//回溯
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -154,7 +154,54 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
// 遍历添加
|
||||||
|
func replaceSpace(s string) string {
|
||||||
|
b := []byte(s)
|
||||||
|
result := make([]byte, 0)
|
||||||
|
for i := 0; i < len(b); i++ {
|
||||||
|
if b[i] == ' ' {
|
||||||
|
result = append(result, []byte("%20")...)
|
||||||
|
} else {
|
||||||
|
result = append(result, b[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 原地修改
|
||||||
|
func replaceSpace(s string) string {
|
||||||
|
b := []byte(s)
|
||||||
|
length := len(b)
|
||||||
|
spaceCount := 0
|
||||||
|
// 计算空格数量
|
||||||
|
for _, v := range b {
|
||||||
|
if v == ' ' {
|
||||||
|
spaceCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 扩展原有切片
|
||||||
|
resizeCount := spaceCount * 2
|
||||||
|
tmp := make([]byte, resizeCount)
|
||||||
|
b = append(b, tmp...)
|
||||||
|
i := length - 1
|
||||||
|
j := len(b) - 1
|
||||||
|
for i >= 0 {
|
||||||
|
if b[i] != ' ' {
|
||||||
|
b[j] = b[i]
|
||||||
|
i--
|
||||||
|
j--
|
||||||
|
} else {
|
||||||
|
b[j] = '0'
|
||||||
|
b[j-1] = '2'
|
||||||
|
b[j-2] = '%'
|
||||||
|
i--
|
||||||
|
j = j - 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user