• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode384.打乱数组|ShuffleanArray

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10283261.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap