1. 程式人生 > >[Swift]LeetCode398. 隨機數索引 | Random Pick Index

[Swift]LeetCode398. 隨機數索引 | Random Pick Index

var ssi have each ability space set ber div

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge.

Example:

int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);

// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3);

// pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);

給定一個可能含有重復元素的整數數組,要求隨機輸出給定的數字的索引。 您可以假設給定的數字一定存在於數組中。

註意:
數組大小可能非常大。 使用太多額外空間的解決方案將不會通過測試。

示例:

int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);

// pick(3) 應該返回索引 2,3 或者 4。每個索引的返回概率應該相等。
solution.pick(3);

// pick(1) 應該返回 0。因為只有nums[0]等於1。
solution.pick(1);

792ms
 1 class Solution {
 2     var v:[Int] = [Int]()
 3 
 4     init(_ nums: [Int]) {
 5         v = nums
 6     }
 7     
 8     func pick(_ target: Int) -> Int {
 9         var cnt:Int = 0
10         var res:Int = -1
11         for i in 0..<v.count
12         {
13             if v[i] != target {continue
} 14 cnt += 1 15 if Int.random(in: 0...Int.max) % cnt == 0 16 { 17 res = i 18 } 19 } 20 return res 21 } 22 } 23 24 /** 25 * Your Solution object will be instantiated and called as such: 26 * let obj = Solution(nums) 27 * let ret_1: Int = obj.pick(target) 28 */ 29

824ms

 1 class Solution {
 2 
 3     let nums: [Int]
 4     let numSize: Int
 5     
 6     init(_ nums: [Int]) {
 7         self.nums = nums
 8         numSize = nums.count
 9     }
10     
11     func pick(_ target: Int) -> Int {
12         for i in 0..<numSize {
13             let idx = Int.random(in: 0..<numSize-i) + i
14             if nums[idx] == target {
15                 return idx
16             }
17             if nums[i] == target {
18                 return i
19             }
20         }
21         
22         return 0
23     }
24 }

916ms

 1 class Solution {
 2     var map:[Int: [Int]] = [:]
 3     
 4     init(_ nums: [Int]) {
 5         map = [:]
 6         for i in 0 ..< nums.count {
 7             var array = map[nums[i]]
 8             if array == nil {
 9                 array = []
10             }
11             array!.append(i)
12             map[nums[i]] = array
13         }
14     }
15     
16     func pick(_ target: Int) -> Int {
17         if let array = map[target] {
18             let size = array.count
19             let r = Int.random(in: 0 ..< size)
20             return array[r]
21         }
22         return -1
23     }
24 }

940ms

 1 class Solution {
 2     var d: [Int: [Int]] = [:]
 3 
 4     init(_ nums: [Int]) {
 5         for (i, num) in nums.enumerated() {
 6             if let some = d[num] {
 7                 d[num] = some + [i]
 8             } else {
 9                 d[num] = [i]
10             }
11         }
12     }
13     
14     func pick(_ target: Int) -> Int {
15         let arr = d[target]!
16         var randI = Int.random(in: 0..<arr.count)
17         return arr[randI]
18     }
19 }

952ms

 1 class Solution {
 2     
 3     private let numsIndices: [Int: [Int]]
 4 
 5     init(_ nums: [Int]) {
 6         var idxMap = [Int: [Int]]()
 7         for (offset, num) in nums.enumerated() {
 8             if idxMap[num] == nil { idxMap[num] = [Int]() }
 9             idxMap[num]?.append(offset)
10         }
11         numsIndices = idxMap
12     }
13     
14     func pick(_ target: Int) -> Int {
15         return numsIndices[target]!.randomElement()!
16     }
17 }

1020ms

 1 class Solution {
 2 
 3     var mapping = [Int: [Int]]()//key is the number, value is the index array
 4     init(_ nums: [Int]) {
 5         for i in 0..<nums.count{
 6             if var indexArray = mapping[nums[i]]{
 7                 indexArray.append(i)
 8                 mapping[nums[i]] =  indexArray
 9             } else{
10                 mapping[nums[i]] = [i]
11             }
12         }
13     }
14     
15     func pick(_ target: Int) -> Int {
16         if let indexArray = mapping[target]{
17             return indexArray.randomElement()!
18         }
19         return -1      
20     }
21 }

1172ms

 1 class Solution {
 2     
 3     var nums: [Int]
 4     
 5     init(_ nums: [Int]) {
 6         self.nums = nums
 7     }
 8     
 9     func pick(_ target: Int) -> Int {
10         let array = self.nums.enumerated().shuffled()
11         for i in stride(from: 0, to: array.count, by: 1) {
12             if i >= array.count {
13                 return -1
14             }
15             if array[i].element == target {
16                 print(array[i].offset)
17                 return array[i].offset
18             }
19         }
20         return -1 
21     }
22 }

[Swift]LeetCode398. 隨機數索引 | Random Pick Index