[leetcode] 398. Random Pick Index
看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直接保存nums数组,和每个元素的数目,然后先生成一个随机的第几个target,然后从nums里面再找这个index,交了,就过了,有套路么?
class Solution {
public:
map<int, int> m;
vector<int> num;
Solution(vector<int> nums) {
num = nums;
for (int i = 0; i < nums.size(); i++) {
m[nums[i]]++;
}
srand( (unsigned)time( NULL ) );
}
int pick(int target) {
int t = m[target];
int index = rand() % t + 1;
t = 0;
for (int i = 0; i < num.size(); i++) {
if(num[i] == target) {
t++;
if(t == index) {
return i;
}
}
}
return 0;
}
};
[leetcode] 398. Random Pick Index的更多相关文章
- [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 - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- 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 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- 多路径配置vlome group共享存储,VG的更新。
1. PV的概念: a) 一块物理磁盘一块物理硬盘在被LVM管理时被称为“物理卷”. b) 在LVM能对其进行管理之前需要在硬盘上产生一些特殊的数据结构,这个过程就是建立 ...
- OpenCV训练分类器制作xml文档
OpenCV训练分类器制作xml文档 (2011-08-25 15:50:06) 转载▼ 标签: 杂谈 分类: 学习 我的问题:有了opencv自带的那些xml人脸检测文档,我们就可以用cvLoad( ...
- 流弊博客集锦(updating)
1.http://ifeve.com/ 2.淘宝的 code project http://code.taobao.org/ http://blog.csdn.net/tenfyguo/article ...
- Tomcat无法部署项目
设置项目的Jdk,compire version 增加java EE 如果有必要,现在项目根目录下放置.mymetadata文件 <?xml version="1.0" en ...
- Tips: compilation and creating new projects on Android 4.0
If you have downloaded android 4.0, you should read the article. $ANDROID_ROOT/tools/android.bat is ...
- JAVA获取CLASSPATH路径--转
ClassLoader提供了两个方法用于从装载的类路径中取得资源: public URL getResource(String name); public InputStream ge ...
- Android Studio配置Dagger2 以及butterknife
一.配置butterknife 在build.gradle(Module)文件中的dependencies模块添加: dependencies { // add butterknife compile ...
- linux语言环境设置
查看linux的支持的语言集合 执行locale命令 LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh LC_CTYPE="zh_CN.UTF-8" LC_NU ...
- linux系统下搭建自己的web服务器
之前在windows 2008 server上搭建了一个用于测试的web服务器,但是在打开网站的时候特别的慢,尤其是图片的加载都会失败,当时以为是路径的问题,但是在服务器上自己打开都特别慢,自己实在找 ...
- Oracle删除重复数据的几种常用方法
1.有可区分的主键,其他数据相同 select * from 表名 dl where dl.id not in (select min(d.id) from 表名 d group by 重复列) 2 ...