随机返还target值的坐标(如果存在多个target).

不太明白为什么这个题是M难度的。

无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了。

要么开始简单点,都存了,选的时候再随机选。

前者各种溢出。。貌似memory在leetcode比较值钱。。就用后者

public class Solution
{
int[] nums;
public Solution(int[] nums)
{
this.nums = nums;
} public int pick(int target)
{
int i = 0;
while(nums[i] != target) i++;
int start = i;
while(i < nums.length && nums[i] == target) i++;
Random rdm = new Random(); return start + rdm.nextInt(i-start);
}
}

有一种思路挺逗的:

public class Solution {
int[] nums;
Random r;
public Solution(int[] nums) {
this.nums = nums;
r = new Random();
} public int pick(int target) {
int size = nums.length;
int i = r.nextInt(size);
while (nums[i] != target) {
i = r.nextInt(size);
} return i;
}
}

无限随机选,选到为止。。。这样确实概率是一样的,不过总觉得很奇怪的样子。。。

398. Random Pick Index的更多相关文章

  1. 398. Random Pick Index - LeetCode

    Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...

  2. [LeetCode] 398. Random Pick Index ☆☆☆

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

  3. 398. Random Pick Index随机pick函数

    [抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...

  4. [LC] 398. Random Pick Index

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

  5. 【LeetCode】398. Random Pick Index 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...

  6. [leetcode] 398. Random Pick Index

    我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...

  7. 398 Random Pick Index 随机数索引

    给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...

  8. [LeetCode] Random Pick Index 随机拾取序列

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

  9. Random Pick Index

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

随机推荐

  1. Sublime Text 3 插件、主题、配置

    换电脑,Sublime Text 3 重新配置一遍,做个记录 1. 下载:http://www.sublimetext.com/3 2. 插件管理器 Package Control (Ctrl + ` ...

  2. java中动态反射

    java中动态反射能达到的效果和python的语法糖很像,能够截获方法的实现,在真实方法调用之前和之后进行修改,甚至能够用自己的实现进行特别的替代,也可以用其实现面向切片的部分功能.动态代理可以方便实 ...

  3. Listview 加载更多

    JQM Listview 加载更多 demo - Warren的个人主页 JQM Listview 加载更多 Demo 测试数据1 测试数据2 测试数据3 测试数据4 显示更多 Page Footer ...

  4. POJ 1837 Balance 01背包

    题目: http://poj.org/problem?id=1837 感觉dp的题目都很难做,这道题如果不看题解不知道憋到毕业能不能做出来,转化成了01背包问题,很神奇.. #include < ...

  5. 第 11 章 桥梁模式【Bridge Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> 今天我要说说我自己,梦想中的我自己,我身价过亿,有两个大公司,一个是房地产公司,一个是服装制造业,这两个公司都很赚钱,天 ...

  6. 今天把登陆注册给改成tab了

    真是解决了一个心头大患,本来以为必须请外包公司的工程师才做,自己研究了下也给解决了. 多亏去年做原型时学习了smarty.css.html这些最基本的网站前端开发的技术.FTP上传下载.linux的基 ...

  7. 【HDOJ】4513 吉哥系列故事——完美队形II

    这题目上学期就看了,不过最近发现可以用马拉车来解,而且还是基本算法. 稍微对回文串成立条件变形一下即可. /* 4513 */ #include <iostream> #include & ...

  8. Learing WCF Chapter1 WCF Services

    WCF ServicesWCF services are the new distributed boundary in an enterprise application—with an empha ...

  9. Electrification Plan(最小生成树)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=50#problem/D 最小生成树模板,注意的是这里有k个发电站,它们不再需要连 ...

  10. 汉企C#面向对象——继承

    public class Shengwu { private string _Name; public string Name { get { return _Name; } set { _Name ...