random-pick-index
Posted 笨鸟居士的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了random-pick-index相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/random-pick-index/ public class Solution { private Map mp; private Random rand; public Solution(int[] nums) { mp = new HashMap(); for (int i=0; i<nums.length; i++) { List lt = (ArrayList)mp.remove(nums[i]); if (lt == null) { lt = new ArrayList(); } lt.add(i); mp.put(nums[i], lt); } rand = new Random(); } public int pick(int target) { List lt = (ArrayList)mp.get(target); return (int)lt.get(rand.nextInt(lt.size())); } } /** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int param_1 = obj.pick(target); */
以上是关于random-pick-index的主要内容,如果未能解决你的问题,请参考以下文章