Easy recursive C++ solution

  • 2
    class Solution {
        bool helper(vector<int> & nums, int k, int sum){
            if (sum==0) return true;
            if (sum<0) return false;
            if (k==0) return false;
            if (helper(nums,k-1,sum-nums[k-1])) return true;
            else return helper(nums,k-1,sum);
        bool canPartition(vector<int>& nums) {
            int n=nums.size();
            int sum=0;
            for (int i=0;i<n;i++){
            if (sum%2!=0) return false;
            return helper(nums,n,sum/2);

  • 0

    @liyouvane i like your solution´╝înice

  • 0

    the backtracking solution is nice understanding,but it will be "Time Limit Exceeded"

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.