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. 《Linux内核原理与分析》教学进程

    目录 2019-2020-1 <Linux内核原理与分析>教学进程 考核方案 第一周: 第二周: 第三周: 第四周: 第五周 第六周 第七周: 第八周 第九周 第十周 第十一周: 第十二周 ...

  2. Kafka 入门1

    Kafka 简介 作为一个消息中间件,Kafka 以高扩展性.高吞吐量等特点,在互联网项目中被广泛采用. 不清楚 Kafka 的同学,可以先看看这篇文章: http://blog.csdn.net/s ...

  3. multer 基础教程(中文版)

    此文档于2016年10月3日翻译时multer的版本是1.2.0,它可能不是最新的! 甚至可能存在翻译错误!你可能需要阅读原版英语README 此文档仅供参考! Multer Multer 是一个 n ...

  4. 【Linux命令】Linux命令后面所接选项和参数的区别

    Linux命令后面所接选项和参数的区别 在使用Linux命令时,有时候后面会跟一些"选项"(options)或"参数"(agruments) 命令格式为: #中 ...

  5. Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(三)

    接着上一篇,我们继续来优化. 直接贴代码了: LambdaHelper.cs using System; using System.Collections.Generic; using System. ...

  6. Maven配置教程详解

    Maven的安装与配置 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载以下maven安装包 解压缩即可 根据你的安装路径配置maven环境变量 ...

  7. 网格弹簧质点系统模拟(Spring-Mass System by Fast Method)附源码(转载)

    转载:  https://www.cnblogs.com/shushen/p/5311828.html 弹簧质点模型的求解方法包括显式欧拉积分和隐式欧拉积分等方法,其中显式欧拉积分求解快速,但积分步长 ...

  8. reduce求和真方便

    1.reduce的用法. reduce是JavaScript中的一个方法,常用于数组求和,接收两个参数,第一个参数为累加函数,第二个参数为初始值,这个初始值是前面那个累加函数的参数.如果不指定初始值, ...

  9. js 设计模式——策略模式

    策略模式(Strategy) 定义:将定义的一组算法封装起来,使其相互之间可以替换.封装的算法具有一定的独立性,不会随客户端的变化而变化 废话不多说,先来个例子 // 例如要写一个计算两个数加减乘除的 ...

  10. CATransform3D 特效详解

    http://blog.sina.com.cn/s/blog_8f5097be0101b91z.html