mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0279.完全平方数.md
This commit is contained in:
@ -331,6 +331,7 @@ var numSquares2 = function(n) {
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
// 先遍历物品
|
||||
function numSquares(n: number): number {
|
||||
const goodsNum: number = Math.floor(Math.sqrt(n));
|
||||
const dp: number[] = new Array(n + 1).fill(Infinity);
|
||||
@ -345,6 +346,20 @@ function numSquares(n: number): number {
|
||||
};
|
||||
```
|
||||
|
||||
```rust
|
||||
// 先遍历背包
|
||||
function numSquares(n: number): number {
|
||||
const dp = Array(n + 1).fill(Infinity)
|
||||
dp[0] = 0;
|
||||
for(let i = 1; i <= n; i++){
|
||||
for(let j = 1; j * j <= i; j++){
|
||||
dp[i] = Math.min(dp[i], dp[i -j * j] + 1)
|
||||
}
|
||||
}
|
||||
return dp[n]
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
|
||||
```rust
|
||||
|
Reference in New Issue
Block a user