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 c95294b

Browse files
authored
feat: add rust solution to lc problem: No.0279 (doocs#1572)
No.0279.Perfect Squares
1 parent ca22a43 commit c95294b

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

‎solution/0200-0299/0279.Perfect Squares/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,43 @@ function numSquares(n: number): number {
279279
}
280280
```
281281

282+
### **Rust**
283+
284+
```rust
285+
impl Solution {
286+
pub fn num_squares(n: i32) -> i32 {
287+
let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
288+
let mut dp = vec![vec![i32::MAX; col + 1]; row + 1];
289+
dp[0][0] = 0;
290+
for i in 1..=row {
291+
for j in 0..=col {
292+
dp[i][j] = dp[i - 1][j];
293+
if j >= i * i {
294+
dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1);
295+
}
296+
}
297+
}
298+
dp[row][col]
299+
}
300+
}
301+
```
302+
303+
```rust
304+
impl Solution {
305+
pub fn num_squares(n: i32) -> i32 {
306+
let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
307+
let mut dp = vec![i32::MAX; col + 1];
308+
dp[0] = 0;
309+
for i in 1..=row {
310+
for j in i * i..=col {
311+
dp[j] = std::cmp::min(dp[j], dp[j - i * i] + 1);
312+
}
313+
}
314+
dp[col]
315+
}
316+
}
317+
```
318+
282319
### **...**
283320

284321
```

‎solution/0200-0299/0279.Perfect Squares/README_EN.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,43 @@ function numSquares(n: number): number {
239239
}
240240
```
241241

242+
### **Rust**
243+
244+
```rust
245+
impl Solution {
246+
pub fn num_squares(n: i32) -> i32 {
247+
let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
248+
let mut dp = vec![vec![i32::MAX; col + 1]; row + 1];
249+
dp[0][0] = 0;
250+
for i in 1..=row {
251+
for j in 0..=col {
252+
dp[i][j] = dp[i - 1][j];
253+
if j >= i * i {
254+
dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1);
255+
}
256+
}
257+
}
258+
dp[row][col]
259+
}
260+
}
261+
```
262+
263+
```rust
264+
impl Solution {
265+
pub fn num_squares(n: i32) -> i32 {
266+
let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
267+
let mut dp = vec![i32::MAX; col + 1];
268+
dp[0] = 0;
269+
for i in 1..=row {
270+
for j in i * i..=col {
271+
dp[j] = std::cmp::min(dp[j], dp[j - i * i] + 1);
272+
}
273+
}
274+
dp[col]
275+
}
276+
}
277+
```
278+
242279
### **...**
243280

244281
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn num_squares(n: i32) -> i32 {
3+
let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
4+
let mut dp = vec![vec![i32::MAX; col + 1]; row + 1];
5+
dp[0][0] = 0;
6+
for i in 1..=row {
7+
for j in 0..=col {
8+
dp[i][j] = dp[i - 1][j];
9+
if j >= i * i {
10+
dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1);
11+
}
12+
}
13+
}
14+
dp[row][col]
15+
}
16+
}

0 commit comments

Comments
(0)

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