384. Shuffle an Array数组洗牌
[抄题]:
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数组洗牌的更多相关文章
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 数组洗牌算法-shuffle
数组洗牌,最近直接的想法是从数组随机取出一个元素,放到另一个数组中,但是这样取出的元素会有重复,必须采取一定的方法保证: 1. 元素不能重复2. 元素被抽取的概率相等,即随机性 数组洗牌经典算法有两种 ...
- POJ 3087 Shuffle'm Up(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- leetcode 384. Shuffle an Array
384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- PythonStudy——如何使输出不换行
python 3.x版本打印不换行格式如下: print(x, end="") # end="" 可使输出不换行.双引号之间的内容就是结束的内容, # 可以是空 ...
- day02-python与变量
1.堆区开辟空间存放 变量值 2.将存放 变量值 空间的地址提供给栈区 3.栈区为变量名开辟空间存放提供来的地址 变量直接相互赋值 定义变量的优化机制 定义变量与重新赋值
- Python脚本模拟僵尸进程与孤儿进程
最近一台机器的systemd内存高达30%多,一直不变,后来排查是僵尸进程,什么是僵尸进程呢,只能google,百度等先了解,然后自己总结了一下,虽然这是基础的东西,但是对于我来说就如新大陆一样.花了 ...
- LTE学习笔记(一)——背景知识
一.标准化组织 无线通信技术的演进离不开一些标准化组织. 1.ITU(International Telecommunication Union) 国际电信联盟,主要任务是制定标准,分配无线频谱资源, ...
- VS调试提示“无法启动程序,“...exe”。系统找不到指定文件
当VS调试提示上图所示的警告时,常用的方法是检查“项目”-“属性”-“配置属性”-“常规”-“输出目录”里的路径 项目”-“属性”-“配置属性”-“链接器”-“常规”-“输出文件”里的路径,是否一致, ...
- java为什么不能根据返回值重载?
我以前对Java中为什么不能根据返回值进行重载,而只能根据方法的参数进行重载非常不理解.比如void f(){}和int f(){},虽然他们有同样的名字,但是很容易区分.如果我这样做,肯定是没问题的 ...
- input输入完成后监听
clearTimeout(serachtimer); serachtimer=setTimeout(function(){ xxxxxxx //这里写处理的方法 },1000)
- Python有趣时刻,这些代码让你大呼"卧槽,怎么会这样"
分享一个实用问题,用python读取Excel并保存字典,如何做? 下面是该同学问题截图和代码 image.png 代码截图是下面这样的 image.png 不知道大家第一眼看了这个代码,什么感受?我 ...
- Java序列化机制原理
Java序列化就是将一个对象转化为一个二进制表示的字节数组,通过保存或则转移这些二进制数组达到持久化的目的.要实现序列化,需要实现java.io.Serializable接口.反序列化是和序列化相 ...
- Spring MVC 之 请求url 带后缀的情况
RequestMappingInfoHandlerMapping 在处理http请求的时候, 如果 请求url 有后缀,如果找不到精确匹配的那个@RequestMapping方法.那么,就把后缀去掉, ...