Skip to content

Commit cc4fa5c

Browse files
committedJun 1, 2022
feat: update solutions to lc problem: No.0473
No.0473.Matchsticks to Square
1 parent c8e6e68 commit cc4fa5c

File tree

7 files changed

+109
-15
lines changed

7 files changed

+109
-15
lines changed
 

‎solution/0400-0499/0473.Matchsticks to Square/README.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class Solution:
6464
if u == len(matchsticks):
6565
return True
6666
for i in range(4):
67+
if i > 0 and edges[i - 1] == edges[i]:
68+
continue
6769
edges[i] += matchsticks[u]
6870
if edges[i] <= x and dfs(u + 1):
6971
return True
@@ -104,6 +106,9 @@ class Solution {
104106
return true;
105107
}
106108
for (int i = 0; i < 4; ++i) {
109+
if (i > 0 && edges[i - 1] == edges[i]) {
110+
continue;
111+
}
107112
edges[i] += matchsticks[u];
108113
if (edges[i] <= x && dfs(u - 1, x, matchsticks, edges)) {
109114
return true;
@@ -138,6 +143,7 @@ public:
138143
if (u == matchsticks.size()) return true;
139144
for (int i = 0; i < 4; ++i)
140145
{
146+
if (i > 0 && edges[i - 1] == edges[i]) continue;
141147
edges[i] += matchsticks[u];
142148
if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true;
143149
edges[i] -= matchsticks[u];
@@ -158,23 +164,62 @@ func makesquare(matchsticks []int) bool {
158164
if s%4 != 0 {
159165
return false
160166
}
161-
sort.Ints(matchsticks)
167+
sort.Sort(sort.Reverse(sort.IntSlice(matchsticks)))
162168
edges := make([]int, 4)
163169
var dfs func(u, x int) bool
164170
dfs = func(u, x int) bool {
165-
if u < 0 {
171+
if u == len(matchsticks) {
166172
return true
167173
}
168174
for i := 0; i < 4; i++ {
175+
if i > 0 && edges[i-1] == edges[i] {
176+
continue
177+
}
169178
edges[i] += matchsticks[u]
170-
if edges[i] <= x && dfs(u-1, x) {
179+
if edges[i] <= x && dfs(u+1, x) {
171180
return true
172181
}
173182
edges[i] -= matchsticks[u]
174183
}
175184
return false
176185
}
177-
return dfs(len(matchsticks)-1, s/4)
186+
return dfs(0, s/4)
187+
}
188+
```
189+
190+
### **Rust**
191+
192+
```rust
193+
impl Solution {
194+
pub fn makesquare(matchsticks: Vec<i32>) -> bool {
195+
let mut matchsticks = matchsticks;
196+
197+
fn dfs(matchsticks: &Vec<i32>, edges: &mut [i32; 4], u: usize, x: i32) -> bool {
198+
if u == matchsticks.len() {
199+
return true;
200+
}
201+
for i in 0..4 {
202+
if i > 0 && edges[i - 1] == edges[i] {
203+
continue;
204+
}
205+
edges[i] += matchsticks[u];
206+
if edges[i] <= x && dfs(matchsticks, edges, u + 1, x) {
207+
return true;
208+
}
209+
edges[i] -= matchsticks[u];
210+
}
211+
false
212+
}
213+
214+
let sum: i32 = matchsticks.iter().sum();
215+
if sum % 4 != 0 {
216+
return false;
217+
}
218+
matchsticks.sort_by(|x, y| y.cmp(x));
219+
let mut edges = [0; 4];
220+
221+
dfs(&matchsticks, &mut edges, 0, sum / 4)
222+
}
178223
}
179224
```
180225

‎solution/0400-0499/0473.Matchsticks to Square/README_EN.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Solution:
4646
if u == len(matchsticks):
4747
return True
4848
for i in range(4):
49+
if i > 0 and edges[i - 1] == edges[i]:
50+
continue
4951
edges[i] += matchsticks[u]
5052
if edges[i] <= x and dfs(u + 1):
5153
return True
@@ -84,6 +86,9 @@ class Solution {
8486
return true;
8587
}
8688
for (int i = 0; i < 4; ++i) {
89+
if (i > 0 && edges[i - 1] == edges[i]) {
90+
continue;
91+
}
8792
edges[i] += matchsticks[u];
8893
if (edges[i] <= x && dfs(u - 1, x, matchsticks, edges)) {
8994
return true;
@@ -118,6 +123,7 @@ public:
118123
if (u == matchsticks.size()) return true;
119124
for (int i = 0; i < 4; ++i)
120125
{
126+
if (i > 0 && edges[i - 1] == edges[i]) continue;
121127
edges[i] += matchsticks[u];
122128
if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true;
123129
edges[i] -= matchsticks[u];
@@ -138,23 +144,26 @@ func makesquare(matchsticks []int) bool {
138144
if s%4 != 0 {
139145
return false
140146
}
141-
sort.Ints(matchsticks)
147+
sort.Sort(sort.Reverse(sort.IntSlice(matchsticks)))
142148
edges := make([]int, 4)
143149
var dfs func(u, x int) bool
144150
dfs = func(u, x int) bool {
145-
if u < 0 {
151+
if u == len(matchsticks) {
146152
return true
147153
}
148154
for i := 0; i < 4; i++ {
155+
if i > 0 && edges[i-1] == edges[i] {
156+
continue
157+
}
149158
edges[i] += matchsticks[u]
150-
if edges[i] <= x && dfs(u-1, x) {
159+
if edges[i] <= x && dfs(u+1, x) {
151160
return true
152161
}
153162
edges[i] -= matchsticks[u]
154163
}
155164
return false
156165
}
157-
return dfs(len(matchsticks)-1, s/4)
166+
return dfs(0, s/4)
158167
}
159168
```
160169

‎solution/0400-0499/0473.Matchsticks to Square/Solution.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ class Solution {
1818
if (u == matchsticks.size()) return true;
1919
for (int i = 0; i < 4; ++i)
2020
{
21+
if (i > 0 && edges[i - 1] == edges[i]) continue;
2122
edges[i] += matchsticks[u];
2223
if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true;
2324
edges[i] -= matchsticks[u];
2425
}
2526
return false;
2627
}
27-
};
28+
};

‎solution/0400-0499/0473.Matchsticks to Square/Solution.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ func makesquare(matchsticks []int) bool {
66
if s%4 != 0 {
77
return false
88
}
9-
sort.Ints(matchsticks)
9+
sort.Sort(sort.Reverse(sort.IntSlice(matchsticks)))
1010
edges := make([]int, 4)
1111
var dfs func(u, x int) bool
1212
dfs = func(u, x int) bool {
13-
if u < 0 {
13+
if u == len(matchsticks) {
1414
return true
1515
}
1616
for i := 0; i < 4; i++ {
17+
if i > 0 && edges[i-1] == edges[i] {
18+
continue
19+
}
1720
edges[i] += matchsticks[u]
18-
if edges[i] <= x && dfs(u-1, x) {
21+
if edges[i] <= x && dfs(u+1, x) {
1922
return true
2023
}
2124
edges[i] -= matchsticks[u]
2225
}
2326
return false
2427
}
25-
return dfs(len(matchsticks)-1, s/4)
26-
}
28+
return dfs(0, s/4)
29+
}

‎solution/0400-0499/0473.Matchsticks to Square/Solution.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ private boolean dfs(int u, int x, int[] matchsticks, int[] edges) {
1919
return true;
2020
}
2121
for (int i = 0; i < 4; ++i) {
22+
if (i > 0 && edges[i - 1] == edges[i]) {
23+
continue;
24+
}
2225
edges[i] += matchsticks[u];
2326
if (edges[i] <= x && dfs(u - 1, x, matchsticks, edges)) {
2427
return true;
@@ -27,4 +30,4 @@ private boolean dfs(int u, int x, int[] matchsticks, int[] edges) {
2730
}
2831
return false;
2932
}
30-
}
33+
}

‎solution/0400-0499/0473.Matchsticks to Square/Solution.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ def dfs(u):
44
if u == len(matchsticks):
55
return True
66
for i in range(4):
7+
if i > 0 and edges[i - 1] == edges[i]:
8+
continue
79
edges[i] += matchsticks[u]
810
if edges[i] <= x and dfs(u + 1):
911
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn makesquare(matchsticks: Vec<i32>) -> bool {
3+
let mut matchsticks = matchsticks;
4+
5+
fn dfs(matchsticks: &Vec<i32>, edges: &mut [i32; 4], u: usize, x: i32) -> bool {
6+
if u == matchsticks.len() {
7+
return true;
8+
}
9+
for i in 0..4 {
10+
if i > 0 && edges[i - 1] == edges[i] {
11+
continue;
12+
}
13+
edges[i] += matchsticks[u];
14+
if edges[i] <= x && dfs(matchsticks, edges, u + 1, x) {
15+
return true;
16+
}
17+
edges[i] -= matchsticks[u];
18+
}
19+
false
20+
}
21+
22+
let sum: i32 = matchsticks.iter().sum();
23+
if sum % 4 != 0 {
24+
return false;
25+
}
26+
matchsticks.sort_by(|x, y| y.cmp(x));
27+
let mut edges = [0; 4];
28+
29+
dfs(&matchsticks, &mut edges, 0, sum / 4)
30+
}
31+
}

0 commit comments

Comments
 (0)