LeetCode 473. Matchsticks to Square(利用位运算枚举子集)

473. Matchsticks to Square

Medium

28136FavoriteShare

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactlyone time.

Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.

Example 1:

Input: [1,1,2,2,2]
Output: true

Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.

 

Example 2:

Input: [3,3,3,3,4]
Output: false

Explanation: You cannot find a way to form a square with all the matchsticks.

 

Note:

  1. The length sum of the given matchsticks is in the range of 0 to 10^9.
  2. The length of the given matchstick array will not exceed 15.

题意:给你一堆长短不一的小木棒,问你这些小木棒能否摆成一个正方形。

题解:两种方法:1.dfs + 减枝; 2.位运算

先说位运算:

结果如下:

class Solution {
public:
    bool makesquare(vector<int>& nums) {
        if (nums.size() < 4) return false;
        int target = 0;
        for (int  i = 0; i < nums.size(); i++){
            target += nums[i];
        }
        if(target % 4) return false;
        else target /= 4;
        vector <int> ok_subset;
        vector <int> ok_half;
        int all = 1 << nums.size();//枚举所有子集
        for (int i = 1; i <= all; i++){
            int sum = 0;
            for (int j = 0; j < nums.size(); j++){
                if((i & (1 << j))){
                    sum += nums[j];
                }
            }
            if(sum == target){
                ok_subset.push_back(i);
            }
        }
        for (int i = 0; i < ok_subset.size(); i++){
            for (int j = i + 1; j < ok_subset.size(); j++){
                if((ok_subset[i] & ok_subset[j]) == 0){
                    ok_half.push_back(ok_subset[i] | ok_subset[j]);
                }
            }
        }
        for (int i = 0; i < ok_half.size(); i++){
            for (int j = i+1; j < ok_half.size(); j++){
                if((ok_half[i] & ok_half[j]) == 0){
                    return true;
                }
            }
        }
        return false;
    }
};

 

方法二:dfs + 减枝

class Solution {
public:
    bool cmp(int a, int b){
        return a > b;
    }
    bool makesquare(vector<int>& nums) {
        int sum = 0;
        for (int i = 0; i < nums.size(); i++){
            sum += nums[i];
        }
        if(sum % 4 || sum == 0) return false;
        sort(nums.rbegin(), nums.rend());
        return dfs(0, 0, 0, 0, 0, sum / 4, nums);
    }
    bool dfs(int index, int s1, int s2, int s3, int s4, int s, vector<int>& nums){
        if(index == nums.size()){
            if(s1 == s2 && s2 == s3 && s3 == s4){
                return true;
            } else {
                return false;
            }
        }
        if(s1 > s || s2 > s || s3 > s || s4 >s){//减枝
            return false;
        }
        for (int i = index; i < nums.size(); i++){
            if(dfs(i+1, s1+nums[i], s2, s3, s4, s, nums)){
                return true;
            } else if(dfs(i+1, s1, s2+nums[i], s3, s4, s, nums)){
                return true;
            } else if(dfs(i+1, s1, s2, s3+nums[i], s4, s, nums)){
                return true;
            } else if(dfs(i+1, s1, s2, s3, s4+nums[i], s, nums)){
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值