Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3264d7a

Browse files
Merge pull request youngyangyang04#2031 from fwqaaq/patch-38
Update 0343.整数拆分.md about rust
2 parents d37658d + bd22ad9 commit 3264d7a

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

‎problems/0343.整数拆分.md‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,29 @@ pub fn integer_break(n: i32) -> i32 {
319319
}
320320
```
321321

322+
贪心:
323+
324+
```rust
325+
impl Solution {
326+
pub fn integer_break(mut n: i32) -> i32 {
327+
match n {
328+
2 => 1,
329+
3 => 2,
330+
4 => 4,
331+
5.. => {
332+
let mut res = 1;
333+
while n > 4 {
334+
res *= 3;
335+
n -= 3;
336+
}
337+
res * n
338+
}
339+
_ => panic!("Error"),
340+
}
341+
}
342+
}
343+
```
344+
322345
### TypeScript
323346

324347
```typescript
@@ -344,27 +367,6 @@ function integerBreak(n: number): number {
344367
};
345368
```
346369

347-
### Rust
348-
349-
```Rust
350-
impl Solution {
351-
fn max(a: i32, b: i32) -> i32{
352-
if a > b { a } else { b }
353-
}
354-
pub fn integer_break(n: i32) -> i32 {
355-
let n = n as usize;
356-
let mut dp = vec![0; n + 1];
357-
dp[2] = 1;
358-
for i in 3..=n {
359-
for j in 1..i - 1 {
360-
dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32));
361-
}
362-
}
363-
dp[n]
364-
}
365-
}
366-
```
367-
368370
### C
369371

370372
```c

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /