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

Commitf2273ad

Browse files
committed
add new files
1 parentf2cc455 commitf2273ad

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎384. Shuffle an Array.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Shuffle a set of numbers without duplicates.
3+
4+
Example:
5+
6+
// Init an array with set 1, 2, and 3.
7+
int[] nums = {1,2,3};
8+
Solution solution = new Solution(nums);
9+
10+
// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
11+
solution.shuffle();
12+
13+
// Resets the array back to its original configuration [1,2,3].
14+
solution.reset();
15+
16+
// Returns the random shuffling of array [1,2,3].
17+
solution.shuffle();
18+
19+
*/
20+
package main
21+
typeSolutionstruct {
22+
arr []int
23+
}
24+
25+
26+
funcConstructor(nums []int)Solution {
27+
returnSolution{arr:nums}
28+
}
29+
30+
31+
/** Resets the array to its original configuration and return it. */
32+
func (this*Solution)Reset() []int {
33+
returnthis.arr
34+
}
35+
36+
37+
/** Returns a random shuffling of the array. */
38+
func (this*Solution)Shuffle() []int {
39+
tmp:=make([]int,len(this.arr))
40+
copy(tmp,this.arr)
41+
fori:=0;i<len(tmp);i++{
42+
r:=rand.Intn(len(tmp))
43+
tmp[i],tmp[r]=tmp[r],tmp[i]
44+
}
45+
returntmp
46+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp