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();

Random random = new Random();

random.nextInt(int i);

 public class Solution {
int[] arr;
Random random; public Solution(int[] nums) {
arr = nums;
random = new Random();
} /** Resets the array to its original configuration and return it. */
public int[] reset() {
return arr;
} /** Returns a random shuffling of the array. */
public int[] shuffle() {
int[] copy = arr.clone(); for (int i=arr.length-1; i>=0; i--) {
int index = random.nextInt(i+1);
int temp = copy[index];
copy[index] = copy[i];
copy[i] = temp;
}
return copy;
}
} /**
* 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();
*/

Leetcode: Shuffle an Array的更多相关文章

  1. [LeetCode] Shuffle an Array 数组洗牌

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

  2. LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)

    LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...

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

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

  4. leetcode 384. Shuffle an Array

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

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

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

  6. Java [Leetcode 384]Shuffle an Array

    题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  7. 384. Shuffle an Array

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

  8. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  9. [Swift]LeetCode384. 打乱数组 | Shuffle an Array

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

随机推荐

  1. MySQL binlog-do-db选项是危险的

    很多人通过 binlog-do-db, binlog-ignore-db, replicate-do-db 和   replicate-ignore-db 来过滤复制(某些数据库), 尽管有些使用, ...

  2. max_allowed_packet

    http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in SELECT val1 FROM tbl1 W ...

  3. Flink - Working with State

    All transformations in Flink may look like functions (in the functional processing terminology), but ...

  4. 错误记录--The import XXX cannot be resolved

    错误:The import XXX cannot be resolved弄了好久,import类的都没问题,但就是报错.选择project --> clean后,OK.如果还不行,删掉全部imp ...

  5. [daily][CentOS][yum] 删除包的同时一同清理掉安装时一起装进来的依赖包

    说起来有点绕口,这个需求是这样的. 就是我yum装A包的时候,同时安装了A的依赖包a1,a2,a3. 当我们使用yum remove A卸载A包的是,a1,a2,a3包并不会一同被卸载掉.如果他们没有 ...

  6. 使用微信JS-SDK 实现 自定义 分享 功能

    微信PC端点击页面,转发给朋友.

  7. Bluetooth HFP介绍

    目录 1. 介绍 1.1 目的 1.2 使用场景 1.3 依赖关系 1.4 协议栈 1.5 角色 2. 应用层 3. 空白章节 4. 互操作性要求 4.1 介绍 4.2 Service Level C ...

  8. FastReport的交叉表实际使用的一个例子

    计算发行-->定义份数月表(打开)出现 PosFraisPaysInput选择时间段后,点击“打印”.这个设计表格,就是交叉表. 交叉表的特点是:数据库是一条一条并列的但是出来的结果却是:横向是 ...

  9. zepto-创建dom

    在使用zepto时,为了使得ajax请求回来的数据添加到页面上之后,不至于失去所绑定的事件. 我一般会采用$()创建dom元素节点,然后添加所需要的class和html等等一系列的样式,最基本的是$( ...

  10. SQL SERVER中非聚集索引的覆盖,连接,交叉,过滤

    1.覆盖索引:select和where中包含的结果集中应存在“非聚集索引列”,这样就不用查找基表了,索引表即可搞定:   2.索引交叉:索引的交叉可以理解成建立多个非聚集索引之间的join,如表实体一 ...