384. Shuffle an Array

random.nextInt(n) 返回[0, n) 的随机数,故要+1;

class Solution {

    private int[] nums;
private Random random; public Solution(int[] nums) {
this.nums = nums;
random = new Random();
} /** Resets the array to its original configuration and return it. */
public int[] reset() {
return nums;
} /** Returns a random shuffling of the array. */
public int[] shuffle() {
if(nums == null) return null; int[] a = nums.clone();
for(int j = 1; j < a.length; j++){
int i = random.nextInt(j + 1);
swap(a, i , j);
}
return a;
} private void swap(int[] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}

398. Random Pick Index

2 : It's probability of selection is 1 * (1/2) * (2/3) = 1/3
3 : It's probability of selection is (1/2) * (2/3) = 1/3
4 : It's probability of selection is just 1/3

class Solution {

    int[] nums;
Random random; public Solution(int[] nums) {
this.nums = nums;
this.random = new Random();
} public int pick(int target) {
int res = -1;
int count = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != target)
continue;
if(random.nextInt(++count) == 0)
res = i;
}
return res;
}
}

<Random> 384 398的更多相关文章

  1. Reservoir Sampling - 蓄水池抽样算法&&及相关等概率问题

    蓄水池抽样——<编程珠玑>读书笔记 382. Linked List Random Node 398. Random Pick Index 从n个数中随机选取m个 等概率随机函数面试题总结 ...

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

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

  3. 398. Random Pick Index - LeetCode

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

  4. 398. Random Pick Index

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

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

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

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

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

  7. [LC] 398. Random Pick Index

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

  8. [leetcode] 398. Random Pick Index

    我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...

  9. 398 Random Pick Index 随机数索引

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

随机推荐

  1. lanyu 激活idea

    编辑 C:\Windows\System32\drivers\etc\hosts 文件,最后面加入下面两行 0.0.0.0 account.jetbrains.com 0.0.0.0 www.jetb ...

  2. 数据仓库005 - 复习Linux shell命令 - crontab调度 sh脚本 后台执行 软连接

    一.crontab调度 对于linux 自带crontab而言,      xxx.sh的一般编写格式以#!/bin/bash 解释器开头,可在脚本中加入: date 但是,shell脚本执行 需要 ...

  3. 在Windows下的virtualenv中搭建Flask+MySQLDb开发环境

    virtualenv和Flask的安装前面已经介绍过了,这里主要讲如何在venv中安装MySQL 安装MySQLdb 下载MySQL-python-1.2.3.win32-py2.7.exe并安装. ...

  4. 手风琴效果 animate

    animate的手风琴效果 <style type="text/css"> * { margin: 0; padding: 0; } ul{ list-style: n ...

  5. HTML+css基础 css选择器的种类

    css选择器的种类 标签   权重是001 类  class权重是0010 相当于255个标签选择器 Id   权重是0100相当于255个类 *通配符   代表所有的标签   权重是0000 后代选 ...

  6. Vue.js 源码分析(二十) 指令篇 v-once指令详解

    数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值,例如:<p>Message: {{ msg }}</p>以后每当msg属性发生了改变,插值处的内 ...

  7. 聊聊Java 虚拟机的“那点事”

    本文的使用方法: 这篇文章是一个总结性质的文章,是我在看完<深入理解 Java 虚拟机>后写的(里面可能会有些不准确的地方,欢迎大家指出),本文从头读到尾就是一个虚拟机大部分知识点的框架, ...

  8. 解决java,C#,php,python MD5加密不一致问题

    JAVA写法 public static String md5Base64(final String str, final String key) throws Exception { if ((nu ...

  9. 【UOJ#82】【UR #7】水题生成器(贪心)

    [UOJ#82][UR #7]水题生成器(贪心) 题面 UOJ 题解 把\(n!\)的所有约数搜出来,这个个数不会很多. 然后从大往小能选则选就好了. #include<iostream> ...

  10. WPF通过不透明蒙板切割显示子控件

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Backspace110/article/ ...