File tree 3 files changed +181
-0
lines changed
solution/0400-0499/0416.Partition Equal Subset Sum
3 files changed +181
-0
lines changed Original file line number Diff line number Diff line change @@ -192,6 +192,80 @@ public:
192
192
};
193
193
```
194
194
195
+ ### ** Rust**
196
+
197
+ ``` rust
198
+ impl Solution {
199
+ #[allow(dead_code)]
200
+ pub fn can_partition (nums : Vec <i32 >) -> bool {
201
+ let mut sum = 0 ;
202
+ for e in & nums {
203
+ sum += * e ;
204
+ }
205
+
206
+ if sum % 2 != 0 {
207
+ return false ;
208
+ }
209
+
210
+ let n = nums . len ();
211
+ let m = (sum / 2 ) as usize ;
212
+ let mut dp : Vec <Vec <bool >> = vec! [vec! [false ; m + 1 ]; n + 1 ];
213
+
214
+ // Initialize the dp vector
215
+ dp [0 ][0 ] = true ;
216
+
217
+ // Begin the actual dp process
218
+ for i in 1 ..= n {
219
+ for j in 0 ..= m {
220
+ dp [i ][j ] = if nums [i - 1 ] as usize > j {
221
+ dp [i - 1 ][j ]
222
+ } else {
223
+ dp [i - 1 ][j ] || dp [i - 1 ][j - nums [i - 1 ] as usize ]
224
+ }
225
+ }
226
+ }
227
+
228
+ dp [n ][m ]
229
+ }
230
+ }
231
+ ```
232
+
233
+ ``` rust
234
+ impl Solution {
235
+ #[allow(dead_code)]
236
+ pub fn can_partition (nums : Vec <i32 >) -> bool {
237
+ let mut sum = 0 ;
238
+ for e in & nums {
239
+ sum += * e ;
240
+ }
241
+
242
+ if sum % 2 != 0 {
243
+ return false ;
244
+ }
245
+
246
+ let m = (sum >> 1 ) as usize ;
247
+
248
+ // Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far
249
+ // Which is actually compressing the 2-D dp vector to 1-D
250
+ let mut dp : Vec <bool > = vec! [false ; m + 1 ];
251
+
252
+ // Initialize the dp vector
253
+ dp [0 ] = true ;
254
+
255
+ // Begin the actual dp process
256
+ for e in & nums {
257
+ // For every num in nums vector
258
+ for i in (* e as usize ..= m ). rev () {
259
+ // Update the current status
260
+ dp [i ] |= dp [i - * e as usize ];
261
+ }
262
+ }
263
+
264
+ dp [m ]
265
+ }
266
+ }
267
+ ```
268
+
195
269
### ** Go**
196
270
197
271
``` go
Original file line number Diff line number Diff line change @@ -165,6 +165,80 @@ public:
165
165
};
166
166
```
167
167
168
+ ### ** Rust**
169
+
170
+ ``` rust
171
+ impl Solution {
172
+ #[allow(dead_code)]
173
+ pub fn can_partition (nums : Vec <i32 >) -> bool {
174
+ let mut sum = 0 ;
175
+ for e in & nums {
176
+ sum += * e ;
177
+ }
178
+
179
+ if sum % 2 != 0 {
180
+ return false ;
181
+ }
182
+
183
+ let n = nums . len ();
184
+ let m = (sum / 2 ) as usize ;
185
+ let mut dp : Vec <Vec <bool >> = vec! [vec! [false ; m + 1 ]; n + 1 ];
186
+
187
+ // Initialize the dp vector
188
+ dp [0 ][0 ] = true ;
189
+
190
+ // Begin the actual dp process
191
+ for i in 1 ..= n {
192
+ for j in 0 ..= m {
193
+ dp [i ][j ] = if nums [i - 1 ] as usize > j {
194
+ dp [i - 1 ][j ]
195
+ } else {
196
+ dp [i - 1 ][j ] || dp [i - 1 ][j - nums [i - 1 ] as usize ]
197
+ }
198
+ }
199
+ }
200
+
201
+ dp [n ][m ]
202
+ }
203
+ }
204
+ ```
205
+
206
+ ``` rust
207
+ impl Solution {
208
+ #[allow(dead_code)]
209
+ pub fn can_partition (nums : Vec <i32 >) -> bool {
210
+ let mut sum = 0 ;
211
+ for e in & nums {
212
+ sum += * e ;
213
+ }
214
+
215
+ if sum % 2 != 0 {
216
+ return false ;
217
+ }
218
+
219
+ let m = (sum >> 1 ) as usize ;
220
+
221
+ // Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far
222
+ // Which is actually compressing the 2-D dp vector to 1-D
223
+ let mut dp : Vec <bool > = vec! [false ; m + 1 ];
224
+
225
+ // Initialize the dp vector
226
+ dp [0 ] = true ;
227
+
228
+ // Begin the actual dp process
229
+ for e in & nums {
230
+ // For every num in nums vector
231
+ for i in (* e as usize ..= m ). rev () {
232
+ // Update the current status
233
+ dp [i ] |= dp [i - * e as usize ];
234
+ }
235
+ }
236
+
237
+ dp [m ]
238
+ }
239
+ }
240
+ ```
241
+
168
242
### ** Go**
169
243
170
244
``` go
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ #[ allow( dead_code) ]
3
+ pub fn can_partition ( nums : Vec < i32 > ) -> bool {
4
+ let mut sum = 0 ;
5
+ for e in & nums {
6
+ sum += * e;
7
+ }
8
+
9
+ if sum % 2 != 0 {
10
+ return false ;
11
+ }
12
+
13
+ let n = nums. len ( ) ;
14
+ let m = ( sum / 2 ) as usize ;
15
+ let mut dp: Vec < Vec < bool > > = vec ! [ vec![ false ; m + 1 ] ; n + 1 ] ;
16
+
17
+ // Initialize the dp vector
18
+ dp[ 0 ] [ 0 ] = true ;
19
+
20
+ // Begin the actual dp process
21
+ for i in 1 ..=n {
22
+ for j in 0 ..=m {
23
+ dp[ i] [ j] = if nums[ i - 1 ] as usize > j {
24
+ dp[ i - 1 ] [ j]
25
+ } else {
26
+ dp[ i - 1 ] [ j] || dp[ i - 1 ] [ j - nums[ i - 1 ] as usize ]
27
+ }
28
+ }
29
+ }
30
+
31
+ dp[ n] [ m]
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments