背包问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
/**
* 背包问题
*/
public boolean canPartition(int[] nums) {
int sum = Arrays.stream(nums).sum();
if (sum % 2 == 1) return false;
sum /= 2;
boolean[][] f = new boolean[nums.length+1][sum+1];
f[0][0] = true;
for (int i = 0; i < nums.length; i++) {
f[i+1][0] = true;
for (int j = 1; j <= sum; j++) {
if (j >= nums[i]) {
f[i+1][j] = f[i][j - nums[i]];
}
f[i+1][j] |= f[i][j];
}
}
return f[nums.length][sum];
}
}

Comments
Recent Posts
计算机网络
进程树
进程管理
Categories
Website Info
Article Count :
4
Total Word Count :
14.4k
Unique Visitors :
Page Views :
Last Update :