mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -392,7 +392,66 @@ var combinationSum2 = function(candidates, target) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
C:
|
||||||
|
```c
|
||||||
|
int* path;
|
||||||
|
int pathTop;
|
||||||
|
int** ans;
|
||||||
|
int ansTop;
|
||||||
|
//记录ans中每一个一维数组的大小
|
||||||
|
int* length;
|
||||||
|
int cmp(const void* a1, const void* a2) {
|
||||||
|
return *((int*)a1) - *((int*)a2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void backTracking(int* candidates, int candidatesSize, int target, int sum, int startIndex) {
|
||||||
|
if(sum >= target) {
|
||||||
|
//若sum等于target,复制当前path进入
|
||||||
|
if(sum == target) {
|
||||||
|
int* tempPath = (int*)malloc(sizeof(int) * pathTop);
|
||||||
|
int j;
|
||||||
|
for(j = 0; j < pathTop; j++) {
|
||||||
|
tempPath[j] = path[j];
|
||||||
|
}
|
||||||
|
length[ansTop] = pathTop;
|
||||||
|
ans[ansTop++] = tempPath;
|
||||||
|
}
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for(i = startIndex; i < candidatesSize; i++) {
|
||||||
|
//对同一层树中使用过的元素跳过
|
||||||
|
if(i > startIndex && candidates[i] == candidates[i-1])
|
||||||
|
continue;
|
||||||
|
path[pathTop++] = candidates[i];
|
||||||
|
sum += candidates[i];
|
||||||
|
backTracking(candidates, candidatesSize, target, sum, i + 1);
|
||||||
|
//回溯
|
||||||
|
sum -= candidates[i];
|
||||||
|
pathTop--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes){
|
||||||
|
path = (int*)malloc(sizeof(int) * 50);
|
||||||
|
ans = (int**)malloc(sizeof(int*) * 100);
|
||||||
|
length = (int*)malloc(sizeof(int) * 100);
|
||||||
|
pathTop = ansTop = 0;
|
||||||
|
//快速排序candidates,让相同元素挨到一起
|
||||||
|
qsort(candidates, candidatesSize, sizeof(int), cmp);
|
||||||
|
|
||||||
|
backTracking(candidates, candidatesSize, target, 0, 0);
|
||||||
|
|
||||||
|
*returnSize = ansTop;
|
||||||
|
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < ansTop; i++) {
|
||||||
|
(*returnColumnSizes)[i] = length[i];
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -408,25 +408,57 @@ class Solution {
|
|||||||
Python:
|
Python:
|
||||||
```Python
|
```Python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
"""二叉树的所有路径 递归法"""
|
||||||
|
|
||||||
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
||||||
path=[]
|
path, result = '', []
|
||||||
res=[]
|
self.traversal(root, path, result)
|
||||||
def backtrace(root, path):
|
return result
|
||||||
if not root:return
|
|
||||||
path.append(root.val)
|
def traversal(self, cur: TreeNode, path: List, result: List):
|
||||||
if (not root.left)and (not root.right):
|
path += str(cur.val)
|
||||||
res.append(path[:])
|
# 如果当前节点为叶子节点,添加路径到结果中
|
||||||
ways=[]
|
if not (cur.left or cur.right):
|
||||||
if root.left:ways.append(root.left)
|
result.append(path)
|
||||||
if root.right:ways.append(root.right)
|
return
|
||||||
for way in ways:
|
|
||||||
backtrace(way,path)
|
if cur.left:
|
||||||
path.pop()
|
self.traversal(cur.left, path + '->', result)
|
||||||
backtrace(root,path)
|
|
||||||
return ["->".join(list(map(str,i))) for i in res]
|
if cur.right:
|
||||||
|
self.traversal(cur.right, path + '->', result)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
"""二叉树的所有路径 迭代法"""
|
||||||
|
|
||||||
|
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
||||||
|
# 题目中节点数至少为1
|
||||||
|
stack, path_st, result = deque([root]), deque(), []
|
||||||
|
path_st.append(str(root.val))
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
cur = stack.pop()
|
||||||
|
path = path_st.pop()
|
||||||
|
# 如果当前节点为叶子节点,添加路径到结果中
|
||||||
|
if not (cur.left or cur.right):
|
||||||
|
result.append(path)
|
||||||
|
if cur.right:
|
||||||
|
stack.append(cur.right)
|
||||||
|
path_st.append(path + '->' + str(cur.right.val))
|
||||||
|
if cur.left:
|
||||||
|
stack.append(cur.left)
|
||||||
|
path_st.append(path + '->' + str(cur.left.val))
|
||||||
|
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
func binaryTreePaths(root *TreeNode) []string {
|
func binaryTreePaths(root *TreeNode) []string {
|
||||||
|
@ -321,7 +321,7 @@ const findTargetSumWays = (nums, target) => {
|
|||||||
|
|
||||||
const sum = nums.reduce((a, b) => a+b);
|
const sum = nums.reduce((a, b) => a+b);
|
||||||
|
|
||||||
if(target > sum) {
|
if(Math.abs(target) > sum) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,16 +258,30 @@ class Solution {
|
|||||||
## Python
|
## Python
|
||||||
|
|
||||||
```python
|
```python
|
||||||
//递归法
|
|
||||||
class Solution:
|
class Solution:
|
||||||
|
"""最大二叉树 递归法"""
|
||||||
|
|
||||||
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
|
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
|
||||||
if not nums: return None //终止条件
|
return self.traversal(nums, 0, len(nums))
|
||||||
root = TreeNode(max(nums)) //新建节点
|
|
||||||
p = nums.index(root.val) //找到最大值位置
|
def traversal(self, nums: List[int], begin: int, end: int) -> TreeNode:
|
||||||
if p > 0: //保证有左子树
|
# 列表长度为0时返回空节点
|
||||||
root.left = self.constructMaximumBinaryTree(nums[:p]) //递归
|
if begin == end:
|
||||||
if p < len(nums): //保证有右子树
|
return None
|
||||||
root.right = self.constructMaximumBinaryTree(nums[p+1:]) //递归
|
|
||||||
|
# 找到最大的值和其对应的下标
|
||||||
|
max_index = begin
|
||||||
|
for i in range(begin, end):
|
||||||
|
if nums[i] > nums[max_index]:
|
||||||
|
max_index = i
|
||||||
|
|
||||||
|
# 构建当前节点
|
||||||
|
root = TreeNode(nums[max_index])
|
||||||
|
|
||||||
|
# 递归构建左右子树
|
||||||
|
root.left = self.traversal(nums, begin, max_index)
|
||||||
|
root.right = self.traversal(nums, max_index + 1, end)
|
||||||
|
|
||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -167,7 +167,33 @@ else {
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int minDistance(String word1, String word2) {
|
||||||
|
int m = word1.length();
|
||||||
|
int n = word2.length();
|
||||||
|
int[][] dp = new int[m+1][n+1];
|
||||||
|
for(int i = 1; i <= m; i++){
|
||||||
|
dp[i][0] = i;
|
||||||
|
}
|
||||||
|
for(int i = 1; i <= n; i++){
|
||||||
|
dp[0][i] = i;
|
||||||
|
}
|
||||||
|
for(int i = 1; i <= m; i++){
|
||||||
|
for(int j = 1; j <= n; j++){
|
||||||
|
int left = dp[i][j-1]+1;
|
||||||
|
int mid = dp[i-1][j-1];
|
||||||
|
int right = dp[i-1][j]+1;
|
||||||
|
if(word1.charAt(i-1) != word2.charAt(j-1)){
|
||||||
|
mid ++;
|
||||||
|
}
|
||||||
|
dp[i][j] = Math.min(left,Math.min(mid,right));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[m][n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目,
|
|||||||
|
|
||||||
## 01 背包
|
## 01 背包
|
||||||
|
|
||||||
有N件物品和一个最多能被重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。
|
有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
Reference in New Issue
Block a user