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. list列表常用方法

    列表是Python中常用的功能,我们知道,列表可以用来存储很多信息,掌握列表的功能有助于我们处理更多的问题,下面来看看列表都具有那些属性:     1.append(self,p_object) de ...

  2. java SE :标准输入/输出

    一 标准设备输入/输出 A 标准输入/输出类 System B 控制台读写类 Console  标准输入/输出类  System 1 标准输入.标准输出.错误输出流 // 标准输入流 public f ...

  3. QT编译发布程序后报错如缺少dll、“应用程序无法正常启动(0xc000007b)”的可能解决方法

    QT编译发布程序后报错如缺少dll.“应用程序无法正常启动(0xc000007b)”的可能解决方法 最近项目要用qt,因为初学没有经验,遇到些小问题常常没什么头绪,也查不到解决方法,刚刚还因为低端错误 ...

  4. 计算机网络应用层之cookie

    一.生活中的cookie 无论你知不知道Cookie是什么,在你的生活中,肯定有使用过它.还记得你使用浏览器浏览网页时,当你要登陆时,网页上有一个记住密码或自动登陆的选项,当你选择时,你就使用了Coo ...

  5. Node.js的Buffer那些你可能不知道的用法

    在大多数介绍Buffer的文章中,主要是围绕数据拼接和内存分配这两方面的.比如我们使用fs模块来读取文件内容的时候,返回的就是一个Buffer: fs.readFile('filename', fun ...

  6. 【.NET】学习SQLite(1)

    前沿 SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存 ...

  7. spectre漏洞代码分析-c代码

    下面一句话转自360: 现代处理器(CPU)的运作机制中存在两个用于加速执行的特性,推测执行( Speculative Execution)和间接分支预测(Indirect Branch Predic ...

  8. HDU 5707 Combine String(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5707 题意: 给你三个字符串 S1, S2, S3, 让你判断 S3 是否恰好由字符串 S1 和 S2 ...

  9. Python并发编程-一个简单的多进程实例

    import time from multiprocessing import Process import os def func(args,args2): #传递参数到进程 print(args, ...

  10. [BZOJ 2821] 作诗

    Link: BZOJ 2821 传送门 Solution: 一道类似区间众数的经典分块 由于个数为偶数这样的条件不能支持快速合并 因此要先$O(n*sqrt(n))$预处理出$pre[i][j]$表示 ...