mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0343.整数拆分.md
This commit is contained in:
@ -319,6 +319,29 @@ pub fn integer_break(n: i32) -> i32 {
|
||||
}
|
||||
```
|
||||
|
||||
贪心:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn integer_break(mut n: i32) -> i32 {
|
||||
match n {
|
||||
2 => 1,
|
||||
3 => 2,
|
||||
4 => 4,
|
||||
5.. => {
|
||||
let mut res = 1;
|
||||
while n > 4 {
|
||||
res *= 3;
|
||||
n -= 3;
|
||||
}
|
||||
res * n
|
||||
}
|
||||
_ => panic!("Error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
@ -344,27 +367,6 @@ function integerBreak(n: number): number {
|
||||
};
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
fn max(a: i32, b: i32) -> i32{
|
||||
if a > b { a } else { b }
|
||||
}
|
||||
pub fn integer_break(n: i32) -> i32 {
|
||||
let n = n as usize;
|
||||
let mut dp = vec![0; n + 1];
|
||||
dp[2] = 1;
|
||||
for i in 3..=n {
|
||||
for j in 1..i - 1 {
|
||||
dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32));
|
||||
}
|
||||
}
|
||||
dp[n]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
|
Reference in New Issue
Block a user