题目描述:

Shuffle a set of numbers without duplicates.

Example:

// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle(); // Resets the array back to its original configuration [1,2,3].
solution.reset(); // Returns the random shuffling of array [1,2,3].
solution.shuffle();

解题思路:

每次往后读取数组的时候,当读到第i个的时候以1/i的概率随机替换1~i中的任何一个数,这样保证最后每个数字出现在每个位置上的概率都是相等的。

证明:

设x元素在第m次的时候出现在位置i的概率是1/m,那么在第m+1次的时候,x仍然待在位置i的概率是 1/m * m/(m+1) = 1/(m+1)

代码描述:

public class Solution {

	private int[] nums = null;
private Random random = null; 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 = (int[])nums.clone();
for(int i = 1; i < nums.length; i++){
int j = random.nextInt(i + 1);
swap(a, i, j);
}
return a;
} private void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int[] param_1 = obj.reset();
* int[] param_2 = obj.shuffle();
*/

  

Java [Leetcode 384]Shuffle an Array的更多相关文章

  1. leetcode 384. Shuffle an Array

    384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...

  2. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  3. 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...

  4. 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  5. 384. Shuffle an Array数组洗牌

    [抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  6. LC 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  7. 384. Shuffle an Array(java,数组全排列,然后随机取)

    题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...

  8. leetcode mock Shuffle an Array

    1. shuffle算法: http://www.cnblogs.com/huaping-audio/archive/2008/09/09/1287985.html 注意:我们一般用的是第二种swap ...

  9. 384 Shuffle an Array 打乱数组

    打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...

随机推荐

  1. 动态规划入门-01背包问题 - poj3624

    2017-08-12 18:50:13 writer:pprp 对于最基础的动态规划01背包问题,都花了我好长时间去理解: poj3624是一个最基本的01背包问题: 题意:给你N个物品,给你一个容量 ...

  2. HTTP-API-DESIGN 怎样设计一个合理的 HTTP API (一)

    这个附件的幻灯片是我最近给团队分享关于设计 HTTP API 的时候,结合 这篇 和我们团队历史上的一些错误,总结出来一些适合内部的经验. 简介. 这次分享主要关注以下几部分: HTTP + JSON ...

  3. TCP状态切换流程

    enum { /* * Description of States: * * TCP_SYN_SENT sent a connection request, waiting for ack * * T ...

  4. spring boot2.1读取 apollo 配置中心2

    第二篇:创建spring boot2.1项目 引用apollo的java客户端jar包 <dependency> <groupId>com.ctrip.framework.ap ...

  5. 通过SSH key获取GitHub上项目,导入到IDEA中

    1.在Windows上安装Git 在Windows上使用Git,可以从Git官网直接下载安装程序,然后按默认选项安装即可 安装完成后,在开始菜单里找到“Git”->“Git Bash”,或者在文 ...

  6. logback MDC 使用

    有时候想在logback日志中打印请求IP.流水号这些信息,可以通过MDC(Mapped Diagnostic Contexts)实现: MDC.put("requestNo", ...

  7. lessc的安装

    win+R,cmd打开命令面板,输入cnpm install -g less,如图,然后就可以通过lessc -v查询lessc的版本,出现版本号证明安装成功

  8. PostgreSQL pg_hba.conf 文件简析

    作者:高张远瞩(HiLoveS) 博客:http://www.cnblogs.com/hiloves/ 转载请保留该信息 最近试用PostgreSQL 9.04,将pg_hba.conf配置的一些心得 ...

  9. TestNG入门--安装和基本介绍

    TestNG介绍 TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit,   功能都差不多, 只是功能更加强大,使用也更方便 Java中已经有一个JUnit的测试框架了.  Tes ...

  10. [洛谷U62364]三次函数极值

    U62364 三次函数极值 题面 给定一个三次函数\(f(x)=a_3x^3+a_2x^2+a_1x+a_0\) 求其极值. 格式 输入包括一行四个整数\(a_3,a_2,a_1,a_0\) 输出包括 ...