0416.cpp (403B)
1 class Solution { 2 public: 3 bool canPartition(vector<int> &nums) { 4 int sum = accumulate(nums.begin(), nums.end(), 0), hsum = sum / 2; 5 if (sum % 2) return false; 6 vector<bool> dp(hsum + 1, false); 7 8 dp[0] = true; 9 for (int num : nums) 10 for (int j = hsum; j >= num; j--) 11 if (dp[j - num]) dp[j] = true; 12 13 return dp.back(); 14 } 15 };