C++ | Leetcode C++题解之第473题火柴拼正方形
题目:
题解:
class Solution {
public:bool makesquare(vector<int>& matchsticks) {int totalLen = accumulate(matchsticks.begin(), matchsticks.end(), 0);if (totalLen % 4 != 0) {return false;}int len = totalLen / 4, n = matchsticks.size();vector<int> dp(1 << n, -1);dp[0] = 0;for (int s = 1; s < (1 << n); s++) {for (int k = 0; k < n; k++) {if ((s & (1 << k)) == 0) {continue;}int s1 = s & ~(1 << k);if (dp[s1] >= 0 && dp[s1] + matchsticks[k] <= len) {dp[s] = (dp[s1] + matchsticks[k]) % len;break;}}}return dp[(1 << n) - 1] == 0;}
};