[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 ...
随机推荐
- Asp.Net事务和异常处理:
Asp.Net事务和异常处理: 一.什么是事务处理? 事务处理是一组组和成逻辑工作单元的数据库操作,虽然系统中可能会出错,但事务将控制和维护每个数据库的一致性和完整性. 如果在事务过程中没有遇到错误, ...
- discuz!版本号信息改动步骤
建完网站后,就到了改动discuz! 论坛的步骤了,,将其改动为自己喜欢的样子.是非常有意思的,废话不多说了.以下给大家介绍改动的方法. 1.[改动后台-首页的版权]打开ftp.连接网站,进入到:/f ...
- [ExtJS5学习笔记]第十节 Extjs5新增特性之ViewModel和DataBinding
本文地址:http://blog.csdn.net/sushengmiyan/article/details/38612721 本文作者:sushengmiyan ------------------ ...
- Android多媒体-人脸识别
1. 相关背景 Google 于2006年8月收购Neven Vision 公司 (该公司拥有 10 多项应用于移动设备领域的图像识别的专利),以此获得了图像识别的技术,并不是常快应用到免费的 Pic ...
- 关于unity3d播放flash动画,使用插件uniswf
主要就是代码了. 1.using UnityEngine; using System.Collections; using pumpkin.swf; using System.Collections. ...
- TQ210裸机编程(3)——按键(查询法)
首先查看TQ210的底板原理图 这次编程只操作KEY1和KEY2,在TQ210核心板原理图中搜索XEINT0 可以看出KEY1和KEY2分别接在S5PV210的GPH0_0和GPH0_1引脚. 这次编 ...
- ulimit 命令详解
Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数 可以用ulimit -a 来显示当前的各种用户进程限制. 下面我把某linux用 ...
- [原创]如何写好SqlHelper
所有写数据库应用的都会遇到SqlHelper.每个人实现的也不同,网上现成的例子也很多.但在实际操作中,大部分都不实用.什么样的才是实用的?答:适合应用场景的! 下面来介绍下我写的一个关于Oracle ...
- 文件I/O(不带缓冲)之I/O的效率
程序清单3-3中的程序使用read和write函数复制文件.关于该程序应注意下列各点: 它从标准输入读,写至标准输出,这就假定在执行本程序之前,这些标准输入.输出已由shell安排好.确实,所有常用的 ...
- Android_gridView_LIstener_examle
layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...