[Swift]LeetCode384. 打乱数组 | Shuffle an Array

原文地址:https://www.cnblogs.com/strengthen/p/10283261.html 

Shuffle a set of numbers without duplicates.

Example:

// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle();

// Resets the array back to its original configuration [1,2,3].
solution.reset();

// Returns the random shuffling of array [1,2,3].
solution.shuffle();

打乱一个没有重复元素的数组。

示例:

// 以数字集合 1, 2 和 3 初始化数组。
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。
solution.shuffle();

// 重设数组到它的初始状态[1,2,3]。
solution.reset();

// 随机返回数组[1,2,3]打乱后的结果。
solution.shuffle();

676ms
 1 class Solution {
 2     var nums:[Int]
 3     var oriNums:[Int]
 4     init(_ nums: [Int]) {
 5         self.nums = nums
 6         self.oriNums = nums
 7     }
 8     
 9     /** Resets the array to its original configuration and return it. */
10     func reset() -> [Int] {
11         return self.oriNums   
12     }
13     
14     /** Returns a random shuffling of the array. */
15     func shuffle() -> [Int] {
16         for i in 0..<nums.count/2
17         {
18             var t:Int = Int.random(in:0..<(nums.count))
19             //法1:
20             //(nums[i], nums[t]) = (nums[t],nums[i])
21             //法2:
22             nums.swapAt(i,t);
23         }
24         return nums
25     }  
26 }
27 
28 /**
29  * Your Solution object will be instantiated and called as such:
30  * let obj = Solution(nums)
31  * let ret_1: [Int] = obj.reset()
32  * let ret_2: [Int] = obj.shuffle()
33  */
34  

704ms

 1 class Solution {
 2 
 3     var original: [Int]
 4     var shuffable: [Int]
 5     init(_ nums: [Int]) {
 6         self.original = nums
 7         self.shuffable = nums
 8     }
 9     
10     /** Resets the array to its original configuration and return it. */
11     func reset() -> [Int] {
12       return original
13     }
14     
15     /** Returns a random shuffling of the array. */
16     func shuffle() -> [Int] {
17         shuffable.shuffle()
18         return shuffable
19     }
20 }
21 
22 /**
23  * Your Solution object will be instantiated and called as such:
24  * let obj = Solution(nums)
25  * let ret_1: [Int] = obj.reset()
26  * let ret_2: [Int] = obj.shuffle()
27  */

760ms

 1 class Solution {
 2     var a = [Int]()
 3     init(_ nums: [Int]) {
 4         a = nums
 5     }
 6     
 7     /** Resets the array to its original configuration and return it. */
 8     func reset() -> [Int] {
 9       return a
10     }
11     
12     /** Returns a random shuffling of the array. */
13     func shuffle() -> [Int] {
14         if a == []{return a}
15         return shuffle0()
16     }
17     func shuffle0() -> [Int] {
18         var data:[Int] = a
19         for i in 0..<a.count {
20             let index = Int.random(in: 0 ... i)
21             if index != i {
22                 (data[i] , data[index]) = (data[index] , data[i])
23             }
24         }
25         return data
26     }
27 }
28 
29 /**
30  * Your Solution object will be instantiated and called as such:
31  * let obj = Solution(nums)
32  * let ret_1: [Int] = obj.reset()
33  * let ret_2: [Int] = obj.shuffle()
34  */

828ms

 1 class Solution {
 2     var interges: [Int]
 3     init(_ nums: [Int]) {
 4         self.interges = nums
 5     }
 6     /** Resets the array to its original configuration and return it. */
 7     func reset() -> [Int] {
 8         return interges
 9     }
10     /** Returns a random shuffling of the array. */
11     func shuffle() -> [Int] {
12         var shuffled = interges
13         var cursorL = shuffled.startIndex
14         let cursorR = shuffled.index(before: interges.endIndex)
15         while cursorL < cursorR {
16             let steps = shuffled.distance(from: cursorL, to: cursorR)
17             let randomStep = Int.random(in: 0...steps)
18             let offsetIndex = shuffled.index(cursorL, offsetBy: randomStep)
19             shuffled.swapAt(cursorL, offsetIndex)
20             shuffled.formIndex(after: &cursorL)
21         }
22         return shuffled
23     }
24 }

860ms

 1 class Solution {
 2 var num: [Int]
 3     
 4     init(_ nums: [Int]) {
 5         self.num = nums
 6     }
 7     
 8     /** Resets the array to its original configuration and return it. */
 9     func reset() -> [Int] {
10         return self.num
11     }
12     
13     /** Returns a random shuffling of the array. */
14     func shuffle() -> [Int] {
15         return self.num.shuffled() 
16     }
17 }
原文地址:https://www.cnblogs.com/strengthen/p/10283261.html