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:
@ -775,6 +775,20 @@ var buildTree = function(inorder, postorder) {
|
||||
};
|
||||
```
|
||||
|
||||
从前序与中序遍历序列构造二叉树
|
||||
|
||||
```javascript
|
||||
var buildTree = function(preorder, inorder) {
|
||||
if(!preorder.length)
|
||||
return null;
|
||||
let root = new TreeNode(preorder[0]);
|
||||
let mid = inorder.findIndex((number) => number === root.val);
|
||||
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
|
||||
root.right = buildTree(preorder.slice(mid + 1, preorder.length), inorder.slice(mid + 1, inorder.length));
|
||||
return root;
|
||||
};
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
@ -318,6 +318,57 @@ class Solution {
|
||||
|
||||
Python:
|
||||
|
||||
```Python3
|
||||
class Solution:
|
||||
#1.去除多余的空格
|
||||
def trim_spaces(self,s):
|
||||
n=len(s)
|
||||
left=0
|
||||
right=n-1
|
||||
|
||||
while left<=right and s[left]==' ': #去除开头的空格
|
||||
left+=1
|
||||
while left<=right and s[right]==' ': #去除结尾的空格
|
||||
right=right-1
|
||||
tmp=[]
|
||||
while left<=right: #去除单词中间多余的空格
|
||||
if s[left]!=' ':
|
||||
tmp.append(s[left])
|
||||
elif tmp[-1]!=' ': #当前位置是空格,但是相邻的上一个位置不是空格,则该空格是合理的
|
||||
tmp.append(s[left])
|
||||
left+=1
|
||||
return tmp
|
||||
#2.翻转字符数组
|
||||
def reverse_string(self,nums,left,right):
|
||||
while left<right:
|
||||
nums[left], nums[right]=nums[right],nums[left]
|
||||
left+=1
|
||||
right-=1
|
||||
return None
|
||||
#3.翻转每个单词
|
||||
def reverse_each_word(self, nums):
|
||||
start=0
|
||||
end=0
|
||||
n=len(nums)
|
||||
while start<n:
|
||||
while end<n and nums[end]!=' ':
|
||||
end+=1
|
||||
self.reverse_string(nums,start,end-1)
|
||||
start=end+1
|
||||
end+=1
|
||||
return None
|
||||
|
||||
#4.翻转字符串里的单词
|
||||
def reverseWords(self, s): #测试用例:"the sky is blue"
|
||||
l = self.trim_spaces(s) #输出:['t', 'h', 'e', ' ', 's', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e'
|
||||
self.reverse_string( l, 0, len(l) - 1) #输出:['e', 'u', 'l', 'b', ' ', 's', 'i', ' ', 'y', 'k', 's', ' ', 'e', 'h', 't']
|
||||
self.reverse_each_word(l) #输出:['b', 'l', 'u', 'e', ' ', 'i', 's', ' ', 's', 'k', 'y', ' ', 't', 'h', 'e']
|
||||
return ''.join(l) #输出:blue is sky the
|
||||
|
||||
|
||||
'''
|
||||
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
|
@ -118,8 +118,8 @@ public:
|
||||
|
||||
## 相关题目推荐
|
||||
|
||||
* 904.水果成篮
|
||||
* 76.最小覆盖子串
|
||||
* [904.水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/)
|
||||
* [76.最小覆盖子串](https://leetcode-cn.com/problems/minimum-window-substring/)
|
||||
|
||||
|
||||
|
||||
|
@ -311,6 +311,42 @@ func findMax(nums []int) (index int){
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript版本
|
||||
|
||||
```javascript
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* function TreeNode(val, left, right) {
|
||||
* this.val = (val===undefined ? 0 : val)
|
||||
* this.left = (left===undefined ? null : left)
|
||||
* this.right = (right===undefined ? null : right)
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {TreeNode}
|
||||
*/
|
||||
var constructMaximumBinaryTree = function (nums) {
|
||||
const BuildTree = (arr, left, right) => {
|
||||
if (left > right)
|
||||
return null;
|
||||
let maxValue = -1;
|
||||
let maxIndex = -1;
|
||||
for (let i = left; i <= right; ++i) {
|
||||
if (arr[i] > maxValue) {
|
||||
maxValue = arr[i];
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
let root = new TreeNode(maxValue);
|
||||
root.left = BuildTree(arr, left, maxIndex - 1);
|
||||
root.right = BuildTree(arr, maxIndex + 1, right);
|
||||
return root;
|
||||
}
|
||||
let root = BuildTree(nums, 0, nums.length - 1);
|
||||
return root;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
|
||||
</p>
|
||||
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
# 动态规划:关于01背包问题,你该了解这些!(滚动数组)
|
||||
|
||||
昨天[动态规划:关于01背包问题,你该了解这些!](https://mp.weixin.qq.com/s/FwIiPPmR18_AJO5eiidT6w)中是用二维dp数组来讲解01背包。
|
||||
@ -35,7 +36,7 @@
|
||||
|
||||
**其实可以发现如果把dp[i - 1]那一层拷贝到dp[i]上,表达式完全可以是:dp[i][j] = max(dp[i][j], dp[i][j - weight[i]] + value[i]);**
|
||||
|
||||
**于其把dp[i - 1]这一层拷贝到dp[i]上,不如只用一个一维数组了**,只用dp[j](一维数组,也可以理解是一个滚动数组)。
|
||||
**与其把dp[i - 1]这一层拷贝到dp[i]上,不如只用一个一维数组了**,只用dp[j](一维数组,也可以理解是一个滚动数组)。
|
||||
|
||||
这就是滚动数组的由来,需要满足的条件是上一层可以重复利用,直接拷贝到当前层。
|
||||
|
||||
@ -242,7 +243,24 @@ Java:
|
||||
|
||||
|
||||
Python:
|
||||
```python
|
||||
def test_1_wei_bag_problem():
|
||||
weight = [1, 3, 4]
|
||||
value = [15, 20, 30]
|
||||
bag_weight = 4
|
||||
# 初始化: 全为0
|
||||
dp = [0] * (bag_weight + 1)
|
||||
|
||||
# 先遍历物品, 再遍历背包容量
|
||||
for i in range(len(weight)):
|
||||
for j in range(bag_weight, weight[i] - 1, -1):
|
||||
# 递归公式
|
||||
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
|
||||
|
||||
print(dp)
|
||||
|
||||
test_1_wei_bag_problem()
|
||||
```
|
||||
|
||||
Go:
|
||||
```go
|
||||
|
Reference in New Issue
Block a user