@@ -196,6 +196,69 @@ func combinationSum2(candidates []int, target int) [][]int {
196
196
}
197
197
```
198
198
199
+ ### ** TypeScript**
200
+
201
+ ``` ts
202
+ function combinationSum2(candidates : number [], target : number ): number [][] {
203
+ candidates .sort ((a , b ) => a - b );
204
+ const n = candidates .length ;
205
+ const t: number [] = [];
206
+ const res: number [][] = [];
207
+ const dfs = (i : number , sum : number ) => {
208
+ if (sum > target ) {
209
+ return ;
210
+ }
211
+ if (sum === target ) {
212
+ res .push ([... t ]);
213
+ return ;
214
+ }
215
+ for (let j = i ; j < n ; j ++ ) {
216
+ const num = candidates [j ];
217
+ if (j > i && num === candidates [j - 1 ]) {
218
+ continue ;
219
+ }
220
+ t .push (num );
221
+ dfs (j + 1 , sum + num );
222
+ t .pop ();
223
+ }
224
+ };
225
+ dfs (0 , 0 );
226
+ return res ;
227
+ }
228
+ ```
229
+
230
+ ### ** Rust**
231
+
232
+ ``` rust
233
+ impl Solution {
234
+ fn dfs (i : usize , count : i32 , candidates : & Vec <i32 >, t : & mut Vec <i32 >, res : & mut Vec <Vec <i32 >>) {
235
+ if count < 0 {
236
+ return ;
237
+ }
238
+ if count == 0 {
239
+ res . push (t . clone ());
240
+ return ;
241
+ }
242
+ for j in i .. candidates . len () {
243
+ if j > i && candidates [j ] == candidates [j - 1 ] {
244
+ continue ;
245
+ }
246
+ let num = candidates [j ];
247
+ t . push (num );
248
+ Self :: dfs (j + 1 , count - num , candidates , t , res );
249
+ t . pop ();
250
+ }
251
+ }
252
+
253
+ pub fn combination_sum2 (mut candidates : Vec <i32 >, target : i32 ) -> Vec <Vec <i32 >> {
254
+ candidates . sort ();
255
+ let mut res = Vec :: new ();
256
+ Self :: dfs (0 , target , & candidates , & mut vec! [], & mut res );
257
+ res
258
+ }
259
+ }
260
+ ```
261
+
199
262
### ** ...**
200
263
201
264
```
0 commit comments