398. Random Pick Index
随机返还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的更多相关文章
- 398. Random Pick Index - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- [LeetCode] 398. Random Pick Index ☆☆☆
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 398. Random Pick Index随机pick函数
[抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...
- [LC] 398. Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- [leetcode] 398. Random Pick Index
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
- 398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- node论坛练手
当时学node,自己写了个论坛练手,现在看还是有很多问题,有时间好好改改 https://github.com/hitbs228/countdown
- DISC免费性格测试题
现在给大家推荐一款世界500强和猎头公司招聘人才时用的DISC性格测评,用在找对象方面也比较合适.大家不妨看下自己的性格,就知道该找什么样的意中人啦--- 在每一个大标题中的四个选择题中只选择一个最符 ...
- php curl模拟post请求提交数据
最近在做校园图书馆图书信息的采集程序,既然是图书馆图书的采集,肯定有提交搜索的页面,无非是post提交,让我想到了curl模拟提交,首先通过firebug进行抓包查询下post提交后的格式如下: tx ...
- 动画讲解 Eclipse 常用快捷键
Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的快捷键操作,并且精心录制了动画, 让你一看就会. ...
- DateTimePicker:jQuery日期和时间插件
点击在线预览效果 点击下载该插件 下面是效果截图:
- JavaScript自学代码--(四)
//JavaScript Window - 浏览器对象模型 window.document.getElementById("header"); //等价于 document.get ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...
- android视频播放
视频播放我们用到的是MediaPlayer,显示控件使用的surfaceView 我们向SD卡中先添加个视频文件,我的是xajh.3gp,不要用mp4,MP4会出现 should have subti ...
- 17.2.2 Replication Relay and Status Logs 复制Relay 和状态日志;
17.2.2 Replication Relay and Status Logs 复制Relay 和状态日志: 17.2.2.1 The Slave Relay Log 17.2.2.2 Slave ...
- git从指定的commit创建分支
How do I create a new git branch from an old commit? git checkout -b justin a9c146a09505837ec03b Thi ...