398. Random Pick Index - LeetCode
Question

Solution
思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题
假设输入是:
int[] nums = new int[]{1, 2, 3, 3, 3};
int target = 3;
模拟:
1 != target pass
2 != target pass
3 == target pick的概率 nextInt(1)==0的概率 1,返回这个index的概念是1 - 1/3 -1/3 = 1/3
3 == target pick的概率 nextInt(2)==0的概率 1/2, 返回这个index的概率是1/2 * 2/3 = 1/3
3 == target pick的概率 nextInt(3)==0的概率 1/3, 返回这个index的概率是1/3
Java实现:
class Solution {
private int[] nums;
private Random random;
public Solution(int[] nums) {
this.nums = nums;
random = new Random();
}
public int pick(int target) {
int index = -1;
int count = 0;
for (int i=0; i<nums.length; i++) {
if (nums[i] == target && random.nextInt(++count) == 0) {
index = i;
}
}
return index;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
Reference
398. Random Pick Index - LeetCode的更多相关文章
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- [LeetCode] 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
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
- 398. Random Pick Index
随机返还target值的坐标(如果存在多个target). 不太明白为什么这个题是M难度的. 无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了. 要么开始简单点,都存 ...
- 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 ...
- 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 ...
- Leetcode: Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- ubuntu 20.04 安装 ros1 和ros2
ubuntu 选择Hong Kong 源 1. ROS1安装 添加 sources.list(设置你的电脑可以从 packages.ros.org 接收软件.) sudo sh -c '. /etc ...
- Flex布局在小程序的使用
一篇旧文,上手小程序时做的一些探索 Flex布局是一种十分灵活方便的布局方式,目前主流的现代浏览器基本都实现了对Flex布局的完全支持.而在微信小程序中,IOS端使用的渲染引擎WKWebView和安卓 ...
- php实验一 html网页设计
页面展示: 源码demo: 等我传到github
- JavaScript实现带正则表达式的表单校验(校验成功后跳转)
运行结果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...
- Qt 实现配置 OpenCV 环境,并实现打开图片与调用摄像头
一.说明 所用QT版本:5.9.1 电脑配置:win10,64位系统 调用的是编译好的:OpenCV-MinGW-Build-4.1.0(稍后放链接) 在大学期间,由于项目需求需要用到QT+openc ...
- 时间篇之centos6下修复的ntp操作(ntpd和ntpdate两个服务区别)
系统采样,本采样和命令都是在centos6.4的系统中进行 主要比较centos7和centos6之间的差异,因为大部分都开始采用centos7但是有些老系统还采用centos6,这样我们就需要熟悉c ...
- MySQL 集群历史版本信息
MySQL 集群有两种命名方式,在Mysql5.1版本之前,MySQL 集群是以MySQL版本号命名:MySQL5.1(包括)之后开始以 mysql-mysql_server_version-ndb- ...
- Hadoop-Hive组件部署
一.基础环境 Hive 组件需要基于之前已部署完毕的 Hadoop 全分布系统,在 master 节点上实现 Hive 组件安装. 1.Hadoop-全分布式配置(全部配置) 2.Hadoop-集群运 ...
- Unity—2D边缘检测(描边效果)
一.ShaderLab 1.Alpha值边缘检测 根据图片的Alpha值边缘判定,向内扩一段距离做边缘,颜色设置未描边颜色: 片元着色阶段,向上下左右四个方向做检测,有一个点的透明度为0,判定为边缘: ...
- jquery的常用API
1, 增 $('body').append('<h1>大标题</h1>') $('body').append('<h2>二标题</h2>') $('&l ...