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 ...
随机推荐
- margin系列之内秀篇(二)
本系列摘自 飘零雾雨的博客 可挖掘性 之前已经写过一篇关于 margin 应用场景的文章:margin系列之内秀篇,当然,它的应用场景会远大于文中所述,无法一一列举. 所以本篇权当是对此的补遗好了, ...
- PHP生成制作验证码
看完就会,不会你打我,话不多说.开搞(人狠话不多) 1.0 首先先看代码 <?php header("Content-Type:text/html;Charset=UTF-8" ...
- Yum安装Memcache
rpm -qa | grep libevent yum install libevent -y rpm -qa | grep memcached yum install memcached ...
- datagridview用get,set访问并加锁,可以控制所有使用datagridview的地方都顺序进行访问
public System.Windows.Forms.DataGridView dataGridView1 { get { lock (ojb) { return dataGridView; } } ...
- KVO与KVC初步了解
参考: http://magicalboy.com/kvc_and_kvo/ http://www.mamicode.com/info-detail-515516.html KVC,即是指 NSKey ...
- Mvc学习笔记(4)
上文我介绍了如何将控制器里的值传递给视图,但是是如何传递的呢?原理是什么? 视图 page.cshtml在编译的时候也会编译成一个类,然而这个类会继承于WebViewPage<object> ...
- 关于OA中权限越级的问题
最近被人问了一个问题, 在OA中我, 经理出差了,下属需要用到 经理的权限,应该怎么处理. 这个问题比较简单,大神,请指点一下. 一开始 ,我就被搞懵了. 我的回答是: 经理出差之前赋给权限就可以了. ...
- 转:我们是否应该把后端构建为API
原文来自于:http://www.infoq.com/cn/news/2015/07/api-or-not 不久前,在StackExchange网站上,一位名为SLC的用户提起他正在设计一个ASP.N ...
- [转贴]Linq之动态查询
最近写Linq碰到一个问题, 就是如果要写一个查询系统,这个系统里面有很多TextBox可以填, 然后捞出符合各个字段的数据. 在SQL查询的时候还满方便的,用字符串连连连就可以了, ( 也就是sql ...
- python3使用requests爬取新浪热门微博
微博登录的实现代码来源:https://gist.github.com/mrluanma/3621775 相关环境 使用的python3.4,发现配置好环境后可以直接使用pip easy_instal ...