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

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

时间: 2024-10-11 17:35:26

[Swift]LeetCode384. 打乱数组 | Shuffle an Array的相关文章

[Swift]LeetCode912.排序数组 | Sort an Array

原文链接:https://www.cnblogs.com/strengthen/p/10886335.html Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Output: [1,2,3,5] Example 2: Input: [5,1,1,2,0,0] Output: [0,0,1,1,2,5]  Note: 1 <= A.length <= 1

[LeetCode] Shuffle an Array 数组洗牌

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 lik

php打乱数组二维数组、多维数组

//这个是针对二维数组的!下面针对多维数组的乱序方法<?php function shuffle_assoc($list) { if (!is_array($list)) return $list; $keys = array_keys($list); shuffle($keys); $random = array(); foreach ($keys as $key) $random[$key] = $list[$key]; return $random; } ?> //以下函数也是出自php

简单说说随机打乱数组的方法

原文链接:http://www.gbtags.com/gb/share/5646.htm 把一个数组随机打乱这个需求来源可能就是“洗牌”,所以我们常常称之为洗牌问题.这个问题实现并不复杂,有不少方法可以完成.与其他算法不同,洗牌问题不仅追求速度,还要求“洗得足够开”.今天只想写篇短的,只分享两种比较有代码性的洗牌方法.至于这些方法能不能真正将数组随机打乱,我们下次再讲. 方法一,随机排序法: function shuffle(array) { array.sort(function() { re

Java [Leetcode 384]Shuffle an Array

题目描述: 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 equal

Swift学习—字符串&amp;数组&amp;字典

字符串 OC和Swift中字符串的区别 在OC中字符串类型时NSString,在Swift中字符串类型是String OC中字符串@"",Swift中字符串"" Swift中String是第一个结构体,性能更高 String支持直接遍历 Swift提供了String和NSString之间的无缝转换 字符串的使用 用反斜线 \ 和小括号 () 做字符串插值(把常量\变量插入到字符串中) let hand = 2var age1 = 20let string1 = &q

c# 打乱数组

有时候得到了一个List,我想把它随机排列一下顺序.而且如果针对不同类型的List都能用,就要用到泛型. 其实思想很简单,就是从原List中每次随机取一项,添加到新的List中,并在原List中删除.这样重复,直到原List为空为止. 不过要注意,如果要保护原List不受变化,就必须先Copy一份List,再在Copy上进行操作 public static List<T> GetRandomList<T>(List<T> inputList) { //Copy to a

swift学习之数组、字典、控制流

// Playground - noun: a place where people can play import UIKit //2014-09-23 集合类型 Collection Types //............................................. //1.数组 /* 1. 数组是类型安全的 */ //1.1定义一个数组变量 var shoppingList:[String]=["apple","Eggs"] if sh

【Leetcode】Shuffle an Array

题目链接:https://leetcode.com/problems/shuffle-an-array/ 题目: 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 retur