Skip to content

Commit 344be8b

Browse files
committed
doc: add rust solution to README_EN.md
No.0473.Matchsticks to Square
1 parent cc4fa5c commit 344be8b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

+36
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,42 @@ func makesquare(matchsticks []int) bool {
167167
}
168168
```
169169

170+
### **Rust**
171+
172+
```rust
173+
impl Solution {
174+
pub fn makesquare(matchsticks: Vec<i32>) -> bool {
175+
let mut matchsticks = matchsticks;
176+
177+
fn dfs(matchsticks: &Vec<i32>, edges: &mut [i32; 4], u: usize, x: i32) -> bool {
178+
if u == matchsticks.len() {
179+
return true;
180+
}
181+
for i in 0..4 {
182+
if i > 0 && edges[i - 1] == edges[i] {
183+
continue;
184+
}
185+
edges[i] += matchsticks[u];
186+
if edges[i] <= x && dfs(matchsticks, edges, u + 1, x) {
187+
return true;
188+
}
189+
edges[i] -= matchsticks[u];
190+
}
191+
false
192+
}
193+
194+
let sum: i32 = matchsticks.iter().sum();
195+
if sum % 4 != 0 {
196+
return false;
197+
}
198+
matchsticks.sort_by(|x, y| y.cmp(x));
199+
let mut edges = [0; 4];
200+
201+
dfs(&matchsticks, &mut edges, 0, sum / 4)
202+
}
203+
}
204+
```
205+
170206
### **...**
171207

172208
```

0 commit comments

Comments
 (0)