File tree 1 file changed +36
-0
lines changed
solution/0400-0499/0473.Matchsticks to Square
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,42 @@ func makesquare(matchsticks []int) bool {
167
167
}
168
168
```
169
169
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
+
170
206
### ** ...**
171
207
172
208
```
You can’t perform that action at this time.
0 commit comments