题目描述:

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. [小问题笔记(五)] 用SQL加密字符串(MD5、SHA1),顺便解决读取数据加密后不一样的问题

    这里用到SQL Server内置的函数 HashBytes(). select HashBytes('MD5','bubu') select HashBytes('SHA1','bubu') 以MD5 ...

  2. 通俗易懂讲解IO模型

    前言 说到IO模型,都会牵扯到同步.异步.阻塞.非阻塞这几个词.从词的表面上看,很多人都觉得很容易理解.但是细细一想,却总会发现有点摸不着头脑.自己也曾被这几个词弄的迷迷糊糊的,每次看相关资料弄明白了 ...

  3. Android 版本升级涉及到的数据库数据迁移问题

    最近做老版本向新版本升级,新版本增加了几张表,有的表经过了增加字段.那么如何把老的数据迁移到新的版本里呢? 我写了一段伪代码,是关于我们项目里的 用户登录信息 Users表,    历史表histor ...

  4. 2017 年你应该尝试的 25 个 Android 库

    1.Lottie 由 Airbnb 推出,支持将 Adobe After Effects 动画通过 Bodymovin 导出成 JSON,并在手机上渲染它们.目前已经有超过 8600 颗 star,相 ...

  5. Effective C++学习笔记(1)

    最近刚看完Effective C++,记录一下当前几个比较常用的方法. 1.以独立语句将newed对象置入智能指针 智能指针是以对象管理资源,在构造函数中获得资源并在析构函数中释放资源​ 以下调用:​ ...

  6. tensorflow入门(二)

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #使用numpy生成200个随机点 x_data ...

  7. 安装magento主题模板

    magento 的强大功能自不必说, 另外还有一点更重要的是拥有很多顶级的精致模板开发者和爱好者的支持开发出种类繁多, 用途各异的模板, 深受用户的喜爱, 但是安装模板对于初次使用者或者很久没有使用者 ...

  8. 新添加的DOM节点如何实现动画效果

    如何给新添加的DOM节点加动画效果 最近碰到项目中,在DOM节点中,添加新的 html 后 要有动画效果,一直没能很好地理解,尝试了各种方式,终于找出来了 简化版结构 代码如下 使用jq1.9以上版本 ...

  9. Repeat a string repeat a string

    重要的事情说3遍! 重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. 这是一些对你有帮助的资源: Global String Object 这道题的思路就是按照题目要求一步一步 ...

  10. Sql server日期函数操作

    1.获取前一小时内的数据:DATEADD(HOUR,-1,GETDATE()),将"HOUR"替换成DAY,Month,YEAR就是前一天,前一月,前一年 2.获取日期部分,格式为 ...