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 177eed2

Browse files
Merge pull request youngyangyang04#2064 from fwqaaq/patch-41
Update 0416.分割等和子集.md about rust
2 parents 0f8f8bb + 76d3470 commit 177eed2

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

‎problems/0416.分割等和子集.md‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -406,24 +406,21 @@ var canPartition = function(nums) {
406406

407407
```Rust
408408
impl Solution {
409-
fn max(a: usize, b: usize) -> usize {
410-
if a > b { a } else { b }
411-
}
412409
pub fn can_partition(nums: Vec<i32>) -> bool {
413-
let nums = nums.iter().map(|x| *x as usize).collect::<Vec<usize>>();
414-
let mut sum = 0;
415-
let mut dp: Vec<usize> = vec![0; 10001];
416-
for i in 0..nums.len() {
417-
sum += nums[i];
410+
let sum = nums.iter().sum::<i32>() as usize;
411+
if sum % 2 == 1 {
412+
return false;
418413
}
419-
if sum % 2 == 1 { return false; }
420414
let target = sum / 2;
421-
for i in 0..nums.len() {
422-
for j in (nums[i]..=target).rev() {
423-
dp[j] = Self::max(dp[j], dp[j - nums[i]] + nums[i]);
415+
let mut dp = vec![0; target + 1];
416+
for n in nums {
417+
for j in (n as usize..=target).rev() {
418+
dp[j] = dp[j].max(dp[j - n as usize] + n)
424419
}
425420
}
426-
if dp[target] == target { return true; }
421+
if dp[target] == target as i32 {
422+
return true;
423+
}
427424
false
428425
}
429426
}

0 commit comments

Comments
(0)

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