随机返还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. Swift语言 1小时速学教程

    本文由 张渊杰 (网名寂静)编写 Swift语言 1小时速学教程 写在前面的话 有些人可能想, 呵呵, 1小时学一门语言, 你不是搞笑吧, 我想说, 是的, 完全可以, 就要看你怎么学了 要想在1小时 ...

  2. which命令

    which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果. 格式 which 可执行文件名称 参数 -V  显示版本信息 实例 用 which 去找出 which w ...

  3. facebook快速登录常见错误:后台设置、域名权限、开发模式、公开、沙盒

    开发人员登录地址 :  https://developers.facebook.com/?ref=pf 官方登录API文档地址 : https://developers.facebook.com/do ...

  4. 使用dynamic来简化反射实现

    dynamic是Framework4.0的新特性,dynamic的出现让C#具有了弱语言类型的特性,编译器在编译的时候,不再对类型进行检查,不会报错,但是运行时如果执行的是不存在的属性或者方法,运行程 ...

  5. about hadoop-eclipse-plugin used by IDE

    Apache Hadoop Development Tools (HDT) is still in development phase. So, no official distribution of ...

  6. Windows使用Apache2配置Git服务器

    Windows使用Apache2配置Git服务器 本文地址:http://www.cnblogs.com/cnscoo/p/3373095.html Git下载: 网站:https://code.go ...

  7. WebApi学习总结系列第二篇(webapi的调试)

    目前使用webapi的调试主要有 1.用接口宿主调试.(宿主形式多样:web.winform.还有就是直接用app进行接口调试) 2.用Fiddler抓Http信息,进行调试. 1.用接口宿主调试. ...

  8. git操作回顾:

    1. git查看自己的本地分支: ***:~/mysite/mysite$ git branch * master 2. 查看远程分支: ***:~/mysite/mysite$ git branch ...

  9. Table嵌套去掉子table的外边框

    Table表格去掉子表格的边框 1. 父表格 <table align="center" style="border:none;cell-padding:0; ce ...

  10. uva 11168 - Airport

    凸包+一点直线的知识: #include <cstdio> #include <cmath> #include <cstring> #include <alg ...