8000 can partition · jhideki/rust_leetcode@1277f3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1277f3b

Browse files
committed
can partition
1 parent 602d389 commit 1277f3b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod longest_inc_sub;
1+
mod partition_equal_subset;
22
fn main() {}

src/partition_equal_subset.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub struct Solution {}
2+
impl Solution {
3+
pub fn can_partition(nums: Vec<i32>) -> bool {
4+
use std::collections::HashSet;
5+
let mut target: i32 = nums.iter().sum();
6+
if target % 2 == 0 {
7+
target = target / 2;
8+
} else {
9+
return false;
10+
}
11+
let mut mem: HashSet<i32> = HashSet::new();
12+
mem.insert(0);
13+
for i in (0..nums.len()).rev() {
14+
if mem.contains(&target) {
15+
return true;
16+
}
17+
for val in mem.clone() {
18+
mem.insert(val + nums[i]);
19+
}
20+
}
21+
false
22+
}
23+
}

0 commit comments

Comments
 (0)
0