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. SBT搭建Spark

    http://www.cnblogs.com/yongjian/p/6211007.html http://www.aboutyun.com/thread-8587-1-1.html http://b ...

  2. java RC4加密解密

    package com.dgut.app.utils; import java.lang.Byte; import java.util.UUID; public class RC4 { public ...

  3. ipconfig | find /i "ipv4"

    C:\Users\Bob>ipconfig|find /i "IPv"   本地链接 IPv6 地址. . . . . . . . : fe80::d495:6e3:6368 ...

  4. fasttext和cnn的比较,使用keras imdb看效果——cnn要慢10倍。

    fasttext: '''This example demonstrates the use of fasttext for text classification Based on Joulin e ...

  5. cassandra cqlsh 和 python客户端

    Keyspaces A cluster is a container for keyspaces. A keyspace is the outermost container for data in ...

  6. UVALive 3708 Graveyard(思维题)

    将原有的每个雕塑的坐标位置,映射在一个总长为n+m的数轴上,设第一个点的坐标为0,(新的等分点必然有至少有一个和原来n等分的等分点重合,因为等分点可以等距的绕圆周旋转,总可以转到有至少一个重合的,不妨 ...

  7. IO编程、操作文件或目录、序列化、JSON

    IO中指Input/Output,即输入和输出:涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口 1.由于CPU和内存的速度远远高于外设的速度,所以,在IO编程中,存在速度严重不匹配问题.eg ...

  8. 转:STL迭代器失效问题

    . 对于关联容器(如map, set, multimap,multiset),删除当前的iterator,仅仅会使当前的iterator失效,只要在erase时,递增当前iterator即可.这是因为 ...

  9. THUPC2018 城市地铁规划

    $n$ 个点,你可以随意连成一棵树,一个点的贡献为 $F(度数) \space mod \space 59393$ ,$F$ 为给定多项式函数,不超过 $10$ 次 求这 $n$ 个点的最大贡献,和最 ...

  10. New Year and Counting Cards

    Your friend has n cards. You know that each card has a lowercase English letter on one side and a di ...