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 ...
随机推荐
- 【转】CHAR CHARACTER VARCHAR NCHAR NVARCHAR NVARCHAR2区别
http://blog.csdn.net/lhl6688/article/details/44156823?ref=myread oracle提供了五种字符数据类型:char.nchar.varcha ...
- ubuntu下php开发环境搭建,nginx+(cgi)php5fpm+memcached+xdebug
由于只是开发环境,所以都是选择比较简单的apt-get安装方式 ,但中间也遇到一点问题. 首先安装nginx nginx的安装和配置其实很简单,nginx本身非常轻量级, 直接 sudo apt-ge ...
- PHP模块设计
1.强内聚,功能尽量在类的内部完成 2.弱耦合,开放尽量少的方法给外部调用
- KeepAlive详解
KeepAlive既熟悉又陌生,踩过坑的同学都知道痛.一线运维工程师踩坑之后对于KeepAlive的总结,你不应该错过! 最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通 ...
- nvarchar类型自动增长
,Col AS 'XH' + RIGHT('0000' + RTRIM(ID),4)
- UIStackView 简单使用
UIStackView提供了一个高效的接口用于平铺一行或一列的视图组合.对于嵌入到StackView的视图,你不用再添加自动布局的约束了.Stack View管理这些子视图的布局,并帮你自动布局约束. ...
- SQL Server强制删除复制发布
原文地址:http://blog.csdn.net/leamonjxl/article/details/7352208 SQL Server 中 存在以前(系统还原前)的发布内容,使用鼠标->右 ...
- C++的类和对象
#include <iostream> // 预处理命令 using namespace std; class Student{ // 声明一个类,类名为Student private : ...
- python加密解密
from Crypto.Cipher import Blowfish #easy_install pycrypto可以获得
- Visual Studio 2015 Update 1 成功安装后运行 “出现未能正确加载[XXXX]包,此问题可能是由配置更改或安装另一个扩展导致的。” 可能的解决方法
作死装Visual Studio 2015 update 1.安装过程中虽然波澜不惊,但是安装之后运行回报未能正确安装[XXXX]包.找了半天,在stackoverflow中找到了相关的问题,在问题描 ...