@@ -192,6 +192,120 @@ func (this *Solution) Shuffle() []int {
192
192
*/
193
193
```
194
194
195
+ ### ** JavaScript**
196
+
197
+ ``` js
198
+ /**
199
+ * @param {number[]} nums
200
+ */
201
+ const Solution = function (nums ) {
202
+ this .nums = nums || [];
203
+ };
204
+
205
+ /**
206
+ * Resets the array to its original configuration and return it.
207
+ * @return {number[]}
208
+ */
209
+ Solution .prototype .reset = function () {
210
+ return this .nums ;
211
+ };
212
+
213
+ /**
214
+ * Returns a random shuffling of the array.
215
+ * @return {number[]}
216
+ */
217
+ Solution .prototype .shuffle = function () {
218
+ let a = this .nums .slice ();
219
+ for (let i = 0 ; i < a .length ; i++ ) {
220
+ let rand = Math .floor (Math .random () * (a .length - i)) + i;
221
+ let tmp = a[i];
222
+ a[i] = a[rand];
223
+ a[rand] = tmp;
224
+ }
225
+ return a;
226
+ };
227
+
228
+ /**
229
+ * Your Solution object will be instantiated and called as such:
230
+ * var obj = Object.create(Solution).createNew(nums)
231
+ * var param_1 = obj.reset()
232
+ * var param_2 = obj.shuffle()
233
+ */
234
+ ```
235
+
236
+ ### ** TypeScript**
237
+
238
+ ``` ts
239
+ class Solution {
240
+ private nums: number [];
241
+
242
+ constructor (nums : number []) {
243
+ this .nums = nums ;
244
+ }
245
+
246
+ reset(): number [] {
247
+ return this .nums ;
248
+ }
249
+
250
+ shuffle(): number [] {
251
+ const n = this .nums .length ;
252
+ const res = [... this .nums ];
253
+ for (let i = 0 ; i < n ; i ++ ) {
254
+ const j = Math .floor (Math .random () * n );
255
+ [res [i ], res [j ]] = [res [j ], res [i ]];
256
+ }
257
+ return res ;
258
+ }
259
+ }
260
+
261
+ /**
262
+ * Your Solution object will be instantiated and called as such:
263
+ * var obj = new Solution(nums)
264
+ * var param_1 = obj.reset()
265
+ * var param_2 = obj.shuffle()
266
+ */
267
+ ```
268
+
269
+ ### ** Rust**
270
+
271
+ ``` rust
272
+ use rand :: Rng ;
273
+ struct Solution {
274
+ nums : Vec <i32 >,
275
+ }
276
+
277
+ /**
278
+ * `&self` means the method takes an immutable reference.
279
+ * If you need a mutable reference, change it to `&mut self` instead.
280
+ */
281
+ impl Solution {
282
+ fn new (nums : Vec <i32 >) -> Self {
283
+ Self { nums }
284
+ }
285
+
286
+ fn reset (& self ) -> Vec <i32 > {
287
+ self . nums. clone ()
288
+ }
289
+
290
+ fn shuffle (& mut self ) -> Vec <i32 > {
291
+ let n = self . nums. len ();
292
+ let mut res = self . nums. clone ();
293
+ for i in 0 .. n {
294
+ let j = rand :: thread_rng (). gen_range (0 , n );
295
+ res . swap (i , j );
296
+ }
297
+ res
298
+ }
299
+ }
300
+
301
+ /**
302
+ * Your Solution object will be instantiated and called as such:
303
+ * let obj = Solution::new(nums);
304
+ * let ret_1: Vec<i32> = obj.reset();
305
+ * let ret_2: Vec<i32> = obj.shuffle();
306
+ */
307
+ ```
308
+
195
309
### ** ...**
196
310
197
311
```
0 commit comments