3ms as combination sum II


  • 0
    F
        bool canSumTo(const vector<int> &nums, int idx, unsigned int sum)
        {
            if(sum == 0) return true;
            int i = idx;
            while(i < nums.size() && nums[i] <= sum) {
                if(i == idx || nums[i] != nums[idx]) {
                    if(canSumTo(nums, i+1, sum - nums[i])) return true;
                }
                ++i;
            }
            return false;
        }
    
        bool canPartition(vector<int>& nums) {
            unsigned int sum=0;
            for(auto v : nums) {
                sum += v;
            }
            if( sum & 0x1 ) return false;
            sum = sum/2;
            sort(nums.begin(), nums.end());
            return canSumTo(nums, 0, sum);
        }

Log in to reply
 

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