leetcode mock Shuffle an Array
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的更多相关文章
- [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 384. Shuffle an Array
384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)
LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...
- 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- Leetcode: Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [Swift]LeetCode384. 打乱数组 | Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- python操作json数据格式--基础
非常基础的json库的用法,后续添加数据格式.编码等内容 参考文章 json进阶 Python的json模块提供了一种很简单的方式来编码和解码JSON数据. 其中两个主要的函数是 json.dumps ...
- 浅谈BUFF设计
Buff在游戏中无处不在,比如WOW.DOTA.LOL等等,这些精心设计的BUFF,让我们击节赞叹,沉迷其中. 问:BUFF的本质是什么? BUFF 是对一项或多项数据进行瞬间或持续作用的集合.(持续 ...
- PHP学习笔记(一)数组
初始化数组的方法:统一初始化或逐项初始化. 遍历数组的方法: 1.FOR循环 2.DO...WHILE语句 3.WHILE语句 4.foreach foreach($arr as $key=>$ ...
- linux关闭地址空间随机化(ASLR)
转:http://www.xuebuyuan.com/1571079.html 确认ASLR是否已经被打开,"2"表示已经打开 shanks@shanks-ubuntu:/home ...
- (15) go 方法
- HDU 6029 Graph Theory
贪心. 每次找到后面最近的一个能连边的连边. #include <bits/stdc++.h> using namespace std; ; int T,n,k; ],b[],u[]; i ...
- Docker应用系列(二)| 构建Zookeeper集群
本示例基于Centos 7,在阿里云的三台机器上部署zookeeper集群,假设目前使用的账号为release,拥有sudo权限. 由于Docker官方镜像下载较慢,可以开启阿里云的Docker镜像下 ...
- NumPy简明教程(二、数组1)
NumPy数组 NumPy数组是一个多维数组对象,称为ndarray.其由两部分组成: 实际的数据 描述这些数据的元数据 大部分操作仅针对于元数据,而不改变底层实际的数据. 关于NumPy数组有几点必 ...
- p1315构建双塔 dp
From easthong ☆构建双塔 描述 Description 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了 ...
- [ARC053D]2 つの山札
题意:给定排列$a_{1\cdots n},b_{1\cdots n}$,执行以下操作$2n-2$次来生成一个长度为$2n-2$的序列:选择$a,b$之一(选择的序列长度要$\geq2$),删除它的第 ...