[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

用克隆函数产生一个同样的数组

//use 'clone' to produce a same array
this.copy = nums.clone();

产生随机数的几种方法:

通过System.currentTimeMillis()来获取随机数。实际上是获取当前时间毫秒数,它是long类型。使用方法如下:

final long l = System.currentTimeMillis();

通过Math.random()来获取随机数。实际上,它返回的是0(包含)到1(不包含)之间的double值。使用方法如下:

final double d = Math.random();

通过Random对象获取随机数。Random支持的随机值类型包括:boolean, byte, int, long, float, double。
比如,获取[0, 100)之间的int整数。方法如下:

int i2 = random.nextInt(100);

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

不知道数组的大小就不用写出来,声明一下数组名即可

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

in-place也没有那么难,改改index,往里面塞就行了

math.random 和 random类是不同的

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

实现类的solution可能是自制的,要带上参数。

数组必须一个个地打印:

class MyCode {
public static void main (String[] args) {
int[] nums = {1, 2, 3,54,32,55,87,13};
Solution answer = new Solution(nums);
int[] rst = answer.shuffle();
for (int i = 0; i < 8; i++)
System.out.println("rst[i] = " + rst[i]); int[] copy = answer.reset();
for (int i = 0; i < 8; i++)
System.out.println("copy[i] = " + copy[i]);
}

[潜台词] :

// package whatever; // don't place package name!

import java.io.*;
import java.util.*;
import java.lang.*; class Solution {
//unnecessary to declare the space of the array
int[] nums;
int[] copy; public Solution(int[] nums) {
this.nums = nums;
//use 'clone' to produce a same array
this.copy = nums.clone();
} /** Resets the array to its original configuration and return it. */
public int[] reset() {
return copy;
} /** Returns a random shuffling of the array. */
public int[] shuffle() {
//random
Random random = new Random();
//generate a random index j, then swap
for (int i = 0; i < nums.length; i++) {
int j = random.nextInt(i + 1);
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
//return
return nums;
}
} class MyCode {
public static void main (String[] args) {
int[] nums = {1, 2, 3,54,32,55,87,13};
Solution answer = new Solution(nums);
int[] rst = answer.shuffle();
for (int i = 0; i < 8; i++)
System.out.println("rst[i] = " + rst[i]); int[] copy = answer.reset();
for (int i = 0; i < 8; i++)
System.out.println("copy[i] = " + copy[i]);
}
}

384. 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] Shuffle an Array 数组洗牌

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

  3. 数组洗牌算法-shuffle

    数组洗牌,最近直接的想法是从数组随机取出一个元素,放到另一个数组中,但是这样取出的元素会有重复,必须采取一定的方法保证: 1. 元素不能重复2. 元素被抽取的概率相等,即随机性 数组洗牌经典算法有两种 ...

  4. POJ 3087 Shuffle'm Up(洗牌)

    POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 A common pas ...

  5. leetcode 384. Shuffle an Array

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

  6. 384 Shuffle an Array 打乱数组

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

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

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

  8. Java [Leetcode 384]Shuffle an Array

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

  9. 384. Shuffle an Array

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

随机推荐

  1. PythonStudy——三种字符串 Three strings

    # 普通字符串:u'以字符作为输出单位'print(u'abc') # 用于显示 # 二进制字符串:b'' 二进制字符串以字节作为输出单位print(b'abc') # 用于传输 # 原义字符串:r' ...

  2. 装系统时 System clock uses UTC 问题

    装系统也装了至少不下50次了,每次都是傻瓜一样的按照第一印象在弄,从未想过为啥,装到这里的时候,System clock uses UTC 勾不勾呢,每次都是百度,然后装完这一次下一次又忘了,这是没有 ...

  3. Linux打开文件设置

    在某些情况下会要求增加Linux的文件打开数,以增加服务器到处理效率,在Linux下查看最多能打开的文件数量为: cat /proc/sys/fs/file-max 然后设置ulimit -n 文件数 ...

  4. 轻量应用服务器 访问jsp页面就直接下载的问题

    本地localhost 运行可以 用自己的ip不行.出现这个问题实质原因就是Tomcat服务器就没有起到作用,运行不了jsp文件.这个是核心.去排查错误!网上查了好几天了根本没有解决我的我的问题. 1 ...

  5. CSS之a标签锚点

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. JAVA 异常类型结构分析

    JAVA 异常类型结构分析 Throwable 是所有异常类型的基类,Throwable 下一层分为两个分支,Error 和 Exception. Error 和 Exception Error Er ...

  7. 转:通过ASP.Net页面获取域用户名(当前登陆的用户)

    通过ASP.Net页面获取域用户名(当前登陆的用户) 原文地址: https://www.cnblogs.com/fast-michael/archive/2011/03/14/2057954.htm ...

  8. 虚拟机克隆之后,网卡名称从eth0变成eth1之后的解决办法

    使用VMware安装了CentOS虚拟机,克隆之后使用service network restart指令来重新启动网络服务时,会看到有eth0网卡不存在的提示.出现这种现象的原因是,很多Linux d ...

  9. vue+element 实现在表格内插入其他组件,每行数据独立存储

    使用  v-slot row代表当前行

  10. Python的set集合

    set集合也用{}表示,set中的元素是不重复的.无序的,且它里面的元素必须是可hash的(int,str,tuple,bool),set是可变的. 1.使用set去重 m = [1, '] s = ...