mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -507,6 +507,33 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn find_max_form(strs: Vec<String>, m: i32, n: i32) -> i32 {
|
||||||
|
let (m, n) = (m as usize, n as usize);
|
||||||
|
let mut dp = vec![vec![0; n + 1]; m + 1];
|
||||||
|
for s in strs {
|
||||||
|
let (mut one_num, mut zero_num) = (0, 0);
|
||||||
|
for c in s.chars() {
|
||||||
|
match c {
|
||||||
|
'0' => zero_num += 1,
|
||||||
|
'1' => one_num += 1,
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i in (zero_num..=m).rev() {
|
||||||
|
for j in (one_num..=n).rev() {
|
||||||
|
dp[i][j] = dp[i][j].max(dp[i - zero_num][j - one_num] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp[m][n]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
@ -205,6 +205,30 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
```Java
|
||||||
|
//using set, aligned with the unimproved method
|
||||||
|
class Solution {
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
List<Integer> path = new ArrayList<>();
|
||||||
|
public List<List<Integer>> findSubsequences(int[] nums) {
|
||||||
|
backTracking(nums, 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
private void backTracking(int[] nums, int startIndex){
|
||||||
|
if(path.size() >= 2)
|
||||||
|
result.add(new ArrayList<>(path));
|
||||||
|
HashSet<Integer> hs = new HashSet<>();
|
||||||
|
for(int i = startIndex; i < nums.length; i++){
|
||||||
|
if(!path.isEmpty() && path.get(path.size() -1 ) > nums[i] || hs.contains(nums[i]))
|
||||||
|
continue;
|
||||||
|
hs.add(nums[i]);
|
||||||
|
path.add(nums[i]);
|
||||||
|
backTracking(nums, i + 1);
|
||||||
|
path.remove(path.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
|
@ -426,6 +426,43 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
// 先遍历物品
|
||||||
|
fn complete_pack() {
|
||||||
|
let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4);
|
||||||
|
let mut dp = vec![0; bag_size + 1];
|
||||||
|
for (weight, value) in goods {
|
||||||
|
for j in weight..=bag_size {
|
||||||
|
dp[j] = dp[j].max(dp[j - weight] + value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("先遍历物品:{}", dp[bag_size]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先遍历背包
|
||||||
|
fn complete_pack_after() {
|
||||||
|
let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4);
|
||||||
|
let mut dp = vec![0; bag_size + 1];
|
||||||
|
for i in 0..=bag_size {
|
||||||
|
for (weight, value) in &goods {
|
||||||
|
if i >= *weight {
|
||||||
|
dp[i] = dp[i].max(dp[i - weight] + value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("先遍历背包:{}", dp[bag_size]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_complete_pack() {
|
||||||
|
Solution::complete_pack();
|
||||||
|
Solution::complete_pack_after();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user