|
43 | 43 |
|
44 | 44 | <!-- 这里可写通用的实现逻辑 -->
|
45 | 45 |
|
| 46 | +**方法一:排序 + 回溯** |
| 47 | + |
| 48 | +用 $edges[i]$ 记录正方形每条边当前的长度,对于第 $u$ 根火柴,尝试把它加到 $edges[i]$ 每条边,若加入后 $edges[i]$ 不超过正方形期望长度 $x$,则继续往下递归 $u+1$ 根火柴。若所有火柴都能被加入,说明满足拼成正方形的要求。 |
| 49 | + |
| 50 | +这里对 $matchsticks$ 从大到小排序,可以减少搜索次数。 |
| 51 | + |
| 52 | +时间复杂度 $O(4^n)$,其中 $n$ 表示 $matchsticks$ 的长度。每根火柴可以被放入正方形的 $4$ 条边,共有 $n$ 根火柴。 |
| 53 | + |
46 | 54 | <!-- tabs:start -->
|
47 | 55 |
|
48 | 56 | ### **Python3**
|
49 | 57 |
|
50 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
51 | 59 |
|
52 | 60 | ```python
|
53 |
| - |
| 61 | +class Solution: |
| 62 | + def makesquare(self, matchsticks: List[int]) -> bool: |
| 63 | + def dfs(u): |
| 64 | + if u == len(matchsticks): |
| 65 | + return True |
| 66 | + for i in range(4): |
| 67 | + edges[i] += matchsticks[u] |
| 68 | + if edges[i] <= x and dfs(u + 1): |
| 69 | + return True |
| 70 | + edges[i] -= matchsticks[u] |
| 71 | + return False |
| 72 | + |
| 73 | + x, mod = divmod(sum(matchsticks), 4) |
| 74 | + if mod or x < max(matchsticks): |
| 75 | + return False |
| 76 | + edges = [0] * 4 |
| 77 | + matchsticks.sort(reverse=True) |
| 78 | + return dfs(0) |
54 | 79 | ```
|
55 | 80 |
|
56 | 81 | ### **Java**
|
57 | 82 |
|
58 | 83 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
59 | 84 |
|
60 | 85 | ```java
|
| 86 | +class Solution { |
| 87 | + public boolean makesquare(int[] matchsticks) { |
| 88 | + int s = 0, mx = 0; |
| 89 | + for (int v : matchsticks) { |
| 90 | + s += v; |
| 91 | + mx = Math.max(mx, v); |
| 92 | + } |
| 93 | + int x = s / 4, mod = s % 4; |
| 94 | + if (mod != 0 || x < mx) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + Arrays.sort(matchsticks); |
| 98 | + int[] edges = new int[4]; |
| 99 | + return dfs(matchsticks.length - 1, x, matchsticks, edges); |
| 100 | + } |
| 101 | + |
| 102 | + private boolean dfs(int u, int x, int[] matchsticks, int[] edges) { |
| 103 | + if (u < 0) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + for (int i = 0; i < 4; ++i) { |
| 107 | + edges[i] += matchsticks[u]; |
| 108 | + if (edges[i] <= x && dfs(u - 1, x, matchsticks, edges)) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + edges[i] -= matchsticks[u]; |
| 112 | + } |
| 113 | + return false; |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### **C++** |
| 119 | + |
| 120 | +```cpp |
| 121 | +class Solution { |
| 122 | +public: |
| 123 | + bool makesquare(vector<int>& matchsticks) { |
| 124 | + int s = 0, mx = 0; |
| 125 | + for (int& v : matchsticks) |
| 126 | + { |
| 127 | + s += v; |
| 128 | + mx = max(mx, v); |
| 129 | + } |
| 130 | + int x = s / 4, mod = s % 4; |
| 131 | + if (mod != 0 || x < mx) return false; |
| 132 | + sort(matchsticks.begin(), matchsticks.end(), greater<int>()); |
| 133 | + vector<int> edges(4); |
| 134 | + return dfs(0, x, matchsticks, edges); |
| 135 | + } |
| 136 | + |
| 137 | + bool dfs(int u, int x, vector<int>& matchsticks, vector<int>& edges) { |
| 138 | + if (u == matchsticks.size()) return true; |
| 139 | + for (int i = 0; i < 4; ++i) |
| 140 | + { |
| 141 | + edges[i] += matchsticks[u]; |
| 142 | + if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true; |
| 143 | + edges[i] -= matchsticks[u]; |
| 144 | + } |
| 145 | + return false; |
| 146 | + } |
| 147 | +}; |
| 148 | +``` |
61 | 149 |
|
| 150 | +### **Go** |
| 151 | + |
| 152 | +```go |
| 153 | +func makesquare(matchsticks []int) bool { |
| 154 | + s := 0 |
| 155 | + for _, v := range matchsticks { |
| 156 | + s += v |
| 157 | + } |
| 158 | + if s%4 != 0 { |
| 159 | + return false |
| 160 | + } |
| 161 | + sort.Ints(matchsticks) |
| 162 | + edges := make([]int, 4) |
| 163 | + var dfs func(u, x int) bool |
| 164 | + dfs = func(u, x int) bool { |
| 165 | + if u < 0 { |
| 166 | + return true |
| 167 | + } |
| 168 | + for i := 0; i < 4; i++ { |
| 169 | + edges[i] += matchsticks[u] |
| 170 | + if edges[i] <= x && dfs(u-1, x) { |
| 171 | + return true |
| 172 | + } |
| 173 | + edges[i] -= matchsticks[u] |
| 174 | + } |
| 175 | + return false |
| 176 | + } |
| 177 | + return dfs(len(matchsticks)-1, s/4) |
| 178 | +} |
62 | 179 | ```
|
63 | 180 |
|
64 | 181 | ### **...**
|
|
0 commit comments