public class Solution {

    int[] nums;
Random rnd; public Solution(int[] nums) {
this.nums = nums;
this.rnd = new Random();
} public int pick(int target) {
int result = -;
int count = ;
for (int i = ; i < nums.length; i++) {
if (nums[i] != target)
continue;
if (rnd.nextInt(++count) == )
result = i;
} return result;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/

https://leetcode.com/problems/random-pick-index/#/description

leetcode398的更多相关文章

  1. [Swift]LeetCode398. 随机数索引 | Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  2. leetcode398 and leetcode 382 蓄水池抽样算法

    382. 链表随机节点 给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样. 进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? 示 ...

随机推荐

  1. Python staticmethod

    1 @staticmethod 静态方法 when this method is called, we don't pass an instance of the class to it (as we ...

  2. poi读取Excel内容数据

    public static void main(String[] args) { try{ //获取文件输入流 FileInputStream fileIn = new FileInputStream ...

  3. tensorflow实现Minist手写体识别

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #下载MINIST数据集mnist ...

  4. 智课雅思词汇---二十、前缀syn-sym-syl是什么意思

    智课雅思词汇---二十.前缀syn-sym-syl是什么意思 一.总结 一句话总结:l,m,n是可以互换 前缀:sy-, syn-, sym-, syl- [词根含义]:共同,同时 [词根来源]:(s ...

  5. python中模块的引用

    一. 模块的定义 定义 python模块(Module),是一个python文件,以.py结尾,包含了python对象定义和python语句.模块让你能够有逻辑地组织你的python代码段,把相关的代 ...

  6. c#线程中下载文件到本地

    额,太懒了 直接上示例代码... /// <summary> /// 下载文件到本地 2017-05-31 /// </summary> /// <param name= ...

  7. 你妹的IOS7

    第一时间升级到了IOS7,想体验一把,界面卡就算了,付费的软件闪退也就罢了,耗电发烫也算球了!你妹的,连个电话都打不了了,刚说几句话就黑屏了,要你还有啥用了,果断降级,IOS6已经下载完了,刷机去!

  8. android不同的按钮一起点击崩溃解决

    public class ButtonUtils { private static long lastClickTime; public synchronized static boolean isF ...

  9. Android Volley完全解析(四),带你从源码的角度理解Volley

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17656437 经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了,但是 ...

  10. Asp.net工作流workflow实战之给书签命名(四)

    之前我们的书签名字是通过手动录入的方式,在实际开发中要在流程设计的时候定义好: namespace EazyBPMS.WorkFlow { public sealed class SetStepAct ...