Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8c4399f

Browse files
committed
add: Shuffle an Array
1 parente45da7a commit8c4399f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This is the solution collection of my LeetCode problems, most of them are progra
3939
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/)|[JavaScript](./src/range-sum-query-mutable/res.js)|Medium|
4040
|342|[Power of Four](https://leetcode.com/problems/power-of-four/)|[JavaScript](./src/power-of-four/res.js)|Easy|
4141
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|[JavaScript](./src/reverse-string/res.js)|Easy|
42+
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)|[JavaScript](./src/shuffle-an-array/res.js)|Medium|
4243
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)|[JavaScript](./src/sum-of-left-leaves/res.js)|Easy|
4344
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[JavaScript](./src/number-of-segments-in-a-string/res.js)|Easy|
4445
||[]()|[](./src//res.js)|Easy|

‎src/shuffle-an-array/res.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Shuffle a set of numbers without duplicates.
2+
3+
// Example:
4+
5+
// // Init an array with set 1, 2, and 3.
6+
// int[] nums = {1,2,3};
7+
// Solution solution = new Solution(nums);
8+
9+
// // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
10+
// solution.shuffle();
11+
12+
// // Resets the array back to its original configuration [1,2,3].
13+
// solution.reset();
14+
15+
// // Returns the random shuffling of array [1,2,3].
16+
// solution.shuffle();
17+
18+
/**
19+
* res.js
20+
*@authors Joe Jiang (hijiangtao@gmail.com)
21+
*@date 2017-02-28 23:12:37
22+
*@version $Id$
23+
*
24+
*@param {number[]} nums
25+
*/
26+
letSolution=function(nums){
27+
this.nums=nums;
28+
this.length=nums.length;
29+
};
30+
31+
/**
32+
* Resets the array to its original configuration and return it.
33+
*@return {number[]}
34+
*/
35+
Solution.prototype.reset=function(){
36+
returnthis.nums;
37+
};
38+
39+
/**
40+
* Returns a random shuffling of the array.
41+
*@return {number[]}
42+
*/
43+
Solution.prototype.shuffle=function(){
44+
letshufflelist=[]
45+
for(leti=0;i<this.length;i++){
46+
shufflelist.push(this.nums[i]);
47+
}
48+
49+
for(leti=0;i<this.length;i++){
50+
letrandom=(Math.floor(Math.random()*this.length)+i)%this.length,
51+
temp=0;
52+
53+
if(random===i){
54+
continue;
55+
}
56+
temp=shufflelist[i];
57+
shufflelist[i]=shufflelist[random];
58+
shufflelist[random]=temp;
59+
}
60+
61+
returnshufflelist;
62+
};
63+
64+
/**
65+
* Your Solution object will be instantiated and called as such:
66+
* var obj = Object.create(Solution).createNew(nums)
67+
* var param_1 = obj.reset()
68+
* var param_2 = obj.shuffle()
69+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp