我是链接

看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是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的更多相关文章

  1. [LeetCode] 398. Random Pick Index ☆☆☆

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  2. 398. Random Pick Index - LeetCode

    Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...

  3. 【LeetCode】398. Random Pick Index 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...

  4. 398. Random Pick Index

    随机返还target值的坐标(如果存在多个target). 不太明白为什么这个题是M难度的. 无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了. 要么开始简单点,都存 ...

  5. 398. Random Pick Index随机pick函数

    [抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...

  6. [LC] 398. Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  7. 398 Random Pick Index 随机数索引

    给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...

  8. LeetCode 528. Random Pick with Weight

    原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...

  9. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

随机推荐

  1. C#操作符的重载

    操作符也是可以重载的,先总结一下操作符的性质: 如我们所知,操作符在不同的情况下有不同的语义,具体取决于它所操作的类型.例如,操作符“+”在操作数值类型的时候意味着“加”,在操作字符串时意味着“连接” ...

  2. 剑指OFFER之从上往下打印二叉树(九度OJ1523)

    题目描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入的第一行一个整数n(1<=n<=1000, : ...

  3. 【Android - MD】之Snackbar的使用

    Snackbar 是 Android 5.0 新特性--Material Design 中的一个控件,用来代替 Toast ,Snackbar与Toast的主要区别是:Snackbar可以滑动退出,也 ...

  4. TableControl大小变化

    TableControl跟随Form大小变化: 选中TableControl,而不是TablePage,右侧Layout: 可以对其设置居上.居下等位置

  5. TengineWeb服务器项目

  6. SQL Sever——无法连接到(local)。“未配置远程连接”和“请求失败或服务未及时响应”

       攻克了上篇博客提到的"远程过程调用失败(0x800706be)"的问题. 新的问题接踵而至. . . 一.       watermark/2/text/aHR0cDovL2 ...

  7. Android蓝牙操作笔记

    蓝牙是一种支持设备短距离传输数据的无线技术.android在2.0以后提供了这方面的支持. 从查找蓝牙设备到能够相互通信要经过几个基本步骤(本机做为服务器): 1.设置权限 在manifest中配置 ...

  8. 升级ADT22.6后,Android模拟器无法创建

    这 两天,在社区里看到有小伙伴们反应,自己在Eclipse下无法创建Android模拟器的问题.起初,自己也没太在意,我一直使用的是 Genymotion模拟器.然后,问题不解决,总有那么一天会让自己 ...

  9. 获取设置dom属性

    getAttribute():获取dom节点属性,带一个参数,表示要获取的属性使用方法:object.getAttribute("id"); setAttribute():设置do ...

  10. [001]socket通信--server和client实现迭代的简单例子

    server端: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unist ...