mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-greedy
This commit is contained in:
@ -1149,6 +1149,52 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## rust
|
||||
|
||||
106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn build_tree(inorder: Vec<i32>, postorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
if inorder.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut postorder = postorder;
|
||||
let root = postorder.pop().unwrap();
|
||||
let index = inorder.iter().position(|&x| x == root).unwrap();
|
||||
let mut root = TreeNode::new(root);
|
||||
root.left = Self::build_tree(inorder[..index].to_vec(), postorder[..index].to_vec());
|
||||
root.right = Self::build_tree(inorder[index + 1..].to_vec(), postorder[index..].to_vec());
|
||||
Some(Rc::new(RefCell::new(root)))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
105 从前序与中序遍历序列构造二叉树
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn build_tree(preorder: Vec<i32>, inorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
if preorder.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let root = preorder[0];
|
||||
let index = inorder.iter().position(|&x| x == root).unwrap();
|
||||
let mut root = TreeNode::new(root);
|
||||
root.left = Self::build_tree(preorder[1..index + 1].to_vec(), inorder[0..index].to_vec());
|
||||
root.right = Self::build_tree(
|
||||
preorder[index + 1..].to_vec(),
|
||||
inorder[index + 1..].to_vec(),
|
||||
);
|
||||
Some(Rc::new(RefCell::new(root)))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -271,39 +271,64 @@ int main() {
|
||||
### java
|
||||
|
||||
```java
|
||||
public class BagProblem {
|
||||
public static void main(String[] args) {
|
||||
int[] weight = {1, 3, 4};
|
||||
int[] value = {15, 20, 30};
|
||||
int bagsize = 4;
|
||||
testweightbagproblem(weight, value, bagsize);
|
||||
int[] weight = {1,3,4};
|
||||
int[] value = {15,20,30};
|
||||
int bagSize = 4;
|
||||
testWeightBagProblem(weight,value,bagSize);
|
||||
}
|
||||
|
||||
public static void testweightbagproblem(int[] weight, int[] value, int bagsize){
|
||||
int wlen = weight.length, value0 = 0;
|
||||
//定义dp数组:dp[i][j]表示背包容量为j时,前i个物品能获得的最大价值
|
||||
int[][] dp = new int[wlen + 1][bagsize + 1];
|
||||
//初始化:背包容量为0时,能获得的价值都为0
|
||||
for (int i = 0; i <= wlen; i++){
|
||||
dp[i][0] = value0;
|
||||
/**
|
||||
* 动态规划获得结果
|
||||
* @param weight 物品的重量
|
||||
* @param value 物品的价值
|
||||
* @param bagSize 背包的容量
|
||||
*/
|
||||
public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){
|
||||
|
||||
// 创建dp数组
|
||||
int goods = weight.length; // 获取物品的数量
|
||||
int[][] dp = new int[goods][bagSize + 1];
|
||||
|
||||
// 初始化dp数组
|
||||
// 创建数组后,其中默认的值就是0
|
||||
for (int j = weight[0]; j <= bagSize; j++) {
|
||||
dp[0][j] = value[0];
|
||||
}
|
||||
//遍历顺序:先遍历物品,再遍历背包容量
|
||||
for (int i = 1; i <= wlen; i++){
|
||||
for (int j = 1; j <= bagsize; j++){
|
||||
if (j < weight[i - 1]){
|
||||
dp[i][j] = dp[i - 1][j];
|
||||
}else{
|
||||
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]);
|
||||
|
||||
// 填充dp数组
|
||||
for (int i = 1; i < weight.length; i++) {
|
||||
for (int j = 1; j <= bagSize; j++) {
|
||||
if (j < weight[i]) {
|
||||
/**
|
||||
* 当前背包的容量都没有当前物品i大的时候,是不放物品i的
|
||||
* 那么前i-1个物品能放下的最大价值就是当前情况的最大价值
|
||||
*/
|
||||
dp[i][j] = dp[i-1][j];
|
||||
} else {
|
||||
/**
|
||||
* 当前背包的容量可以放下物品i
|
||||
* 那么此时分两种情况:
|
||||
* 1、不放物品i
|
||||
* 2、放物品i
|
||||
* 比较这两种情况下,哪种背包中物品的最大价值最大
|
||||
*/
|
||||
dp[i][j] = Math.max(dp[i-1][j] , dp[i-1][j-weight[i]] + value[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
//打印dp数组
|
||||
for (int i = 0; i <= wlen; i++){
|
||||
for (int j = 0; j <= bagsize; j++){
|
||||
System.out.print(dp[i][j] + " ");
|
||||
|
||||
// 打印dp数组
|
||||
for (int i = 0; i < goods; i++) {
|
||||
for (int j = 0; j <= bagSize; j++) {
|
||||
System.out.print(dp[i][j] + "\t");
|
||||
}
|
||||
System.out.print("\n");
|
||||
System.out.println("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### python
|
||||
|
Reference in New Issue
Block a user