mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0279.完全平方数.md about Rust
This commit is contained in:
@ -345,6 +345,29 @@ function numSquares(n: number): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// 先遍历背包
|
||||||
|
impl Solution {
|
||||||
|
pub fn num_squares(n: i32) -> i32 {
|
||||||
|
let n = n as usize;
|
||||||
|
let mut dp = vec![i32::MAX; n + 1];
|
||||||
|
dp[0] = 0;
|
||||||
|
for i in 0..=n {
|
||||||
|
let mut j = 1;
|
||||||
|
loop {
|
||||||
|
match j * j > i {
|
||||||
|
true => break,
|
||||||
|
false => dp[i] = dp[i].min(dp[i - j * j] + 1),
|
||||||
|
}
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp[n]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
Reference in New Issue
Block a user