原文地址:http://www.javashuo.com/article/p-stphjsep-s.html html
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.app
Example:dom
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);
給定一個可能含有重複元素的整數數組,要求隨機輸出給定的數字的索引。 您能夠假設給定的數字必定存在於數組中。ide
注意:
數組大小可能很是大。 使用太多額外空間的解決方案將不會經過測試。測試
示例:spa
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
824mscode
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 }
916mshtm
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 }
940msblog
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 }