1. shuffle算法:

http://www.cnblogs.com/huaping-audio/archive/2008/09/09/1287985.html

注意:我们一般用的是第二种swap的方法;但是第一种直接选择,然后把最末尾一位填上的方法,也是很好的。只是会需要两倍的空间。

2. Random nextInt(bound)参数表示 0-inclusive,bound-exclusive: [0, bound)

package com.company;

import java.util.LinkedList;
import java.util.List;
import java.util.Random; public class Main {
private int[] orig; public Main(int[] nums) {
orig = nums;
} /** Resets the array to its original configuration and return it. */
public int[] reset() {
return orig;
} /** Returns a random shuffling of the array. */
public int[] shuffle() {
int[] ret = orig.clone();
Random rand = new Random();
for (int i=0; i<ret.length; i++) {
// int value between 0 (inclusive) and the specified value (exclusive)
int r = rand.nextInt(ret.length-i);
if (r != ret.length-i-1) {
int k = ret[r];
ret[r] = ret[ret.length - i - 1];
ret[ret.length - i - 1] = k;
} }
return ret;
} public static void main(String[] args) {
// write your code here
System.out.println("Hello"); int []nums = {1,2,3};
Main obj = new Main(nums);
int[] param_1 = obj.reset();
int[] param_2 = obj.shuffle();
System.out.printf("param_1: %d, %d, %d\n", param_1[0], param_1[1], param_1[2]);
System.out.printf("param_2: %d, %d, %d\n", param_2[0], param_2[1], param_2[2]); }
}

leetcode mock Shuffle an Array的更多相关文章

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

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

  2. leetcode 384. Shuffle an Array

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

  3. Java [Leetcode 384]Shuffle an Array

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

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

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

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

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

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

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

  7. Leetcode: Shuffle an Array

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

  8. 384. Shuffle an Array

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

  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. laravel windows安装(composer)

    1.安装composer参考windows 安装tp5 composer方式 2.先配置好本地虚拟域名,在cmd里面切换到网站根目录 ... 3.安装成功之后,在浏览器输入已配置的虚拟域名我的是,la ...

  2. SQL1:基础

    1.SQL命令类型: 1)DDL:CREATE TABLE/INDEX/VIEW ; ALTER TABLE/INDEX/VIEW ; DROP TABLE/INDEX 2)DML:INSERT,UP ...

  3. open -python操作文件

    一打开文件 二操作文件 三关闭文件 open(文件,模式,编码),打开文件----->0101010(以二进制的方式打开)------>编码(open默认utf-8编码)------> ...

  4. Vue 2.0学习(五)v-bind及class与style绑定

    DOM元素经常会动态地绑定一些class类名或style样式. 基本用法 <div id="app"> <a v-bind:href="url" ...

  5. 二. 创建Series和DataFrame对象

    创建对象 创建Series对象 Series可以通过列表,标量值,字典,ndarray,其他函数来创建 a = pf.Series([1,2,3,4]) # 列表创建 b = pd.Series(25 ...

  6. [CCC 2018] 平衡树

    题面在这里! 根据题目描述就可以直接模拟出一个暴力. 如果把前 n^(1/2) 的树的方案数先一遍 O(n^(3/4)) 暴力预处理出来(其实复杂度并到不了这个级别),然后把n带进来直接暴力算就行了. ...

  7. HihoCoder - 1756 打怪

    题面在这里! 拆成两个部分分别算显然比较简单. 前面一个部分排个序枚举最大值算就好啦. 后面的就相当于把每一种数值的贡献加起来,也可以在排完序之后的a[]上面直接算出来. #include<bi ...

  8. 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈

    秋实大哥去打工 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...

  9. Codeforces Round #300 C. Tourist's Notes 水题

    C. Tourist's Notes Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pr ...

  10. CodeM资格赛1

    题目描述 美团外卖的品牌代言人袋鼠先生最近正在进行音乐研究.他有两段音频,每段音频是一个表示音高的序列.现在袋鼠先生想要在第二段音频中找出与第一段音频最相近的部分. 具体地说,就是在第二段音频中找到一 ...