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 ...
随机推荐
- 计算字符串和文件的MD5值
//计算字符串的MD5值 public string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5CryptoServ ...
- Emmet Documentation
src:http://docs.emmet.io/cheat-sheet/ Emmet Documentation Syntax Child: > nav>ul>li <n ...
- SQL(笔试题)网站上的广告位是轮播的,每天某一广告位最多可轮播的广告数量是有限制的,比如A广告位,每天只能轮播三个广告,但销售人员在销售广告位时并不考虑此限制,要求查询出合同表中,超过广告位轮播数量的合同。
合同表 Orders OrderID Positioncode Startdate Enddate 1 A 2006-11-01 2006-11-03 2 C 2006-11-02 2006-11-0 ...
- Html DOM 常用属性和方法
Node对象的节点类型***************************************************接口 nodeType常量 nodeType值 备注Element Node ...
- git 常用命令行整理
1.创建分支dev git branch dev 2.切换到dev分支 git checkout dev 3.创建并切换本地分支,分支名为dev git checkout -b dev 4.查看本地所 ...
- JAVA 时间差距,两个时间相差多少天,时,分,秒
JAVA 时间差距,两个时间相差多少天,时,分,秒 package io; import java.text.DateFormat; import java.text.ParseException; ...
- check、continue、exit的区别
DATA:BEGIN OF lt_table OCCURS 0, i_row TYPE i, i_col TYPE i, END OF lt_table. lt_t ...
- 3D触控简介:建立数字刻度应用及快速活动栏
苹果公司通过 iPhone 6s 和 6s Plus 引入了与手机互动的全新方式:按压手势.你也许知道,苹果智能手表和苹果笔记本电脑早已具备这一功能,只是名称略有不同,为力感触控(Force Touc ...
- 【POJ1743】 Musical Theme (二分+后缀数组)
Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)note ...
- linux使用su切换用户提示 Authentication failure的解决方法& 复制文件时,报cp: omitting directory `XXX'
linux使用su切换用户提示 Authentication failure的解决方法:这个问题产生的原因是由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...