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. zookeeper的简单使用

    前言 最近项目中要使用基于zookeeper的集中配置管理系统,而对于zookeeper仅在当初使用阿里开源分布式服务调用框架dubbo时简单的了解一下.本 文的主要目的,调用zkclient (ma ...

  2. Python爬虫-正则表达式基础

    import re #常规匹配 content = 'Hello 1234567 World_This is a Regex Demo' #result = re.match('^Hello\s\d\ ...

  3. lr_get_transaction_duration 函数介绍

    lr_get_transaction_duration 用于获取事务所消耗的时间. 实例: Action() { double trans_time; //定义变量 web_url("www ...

  4. LoadRunner中InvokeMethod failure: 外部组件发生异常解决办法

    -82801 abnormal termination,caused by mdrv process termination -29996 error:process mmdrv.exe was no ...

  5. bzoj 1818 主席树

    思路:主席树搞一搞. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #de ...

  6. openssl 获取证书中的公钥

    PEM 格式 1.  FILE *fp = fopen("xx.pem", "r"); 2.  X509 *cert = PEM_read_X509(fp, N ...

  7. 【笔试题】怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串?

    笔试题 怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串? import java.io.UnsupportedEncodingException; public clas ...

  8. HashMap实现原理及常见问题

    1.简介 HashMap是基于哈希表的Map接口的实现,用来存放键值对(Entry<Key,Value>),并提供可选的映射操作.使用put(Key,Value)存储对象到HashMap中 ...

  9. dp 动规 最佳加法表达式

    最佳加法表达式 有一个由1..9组成的数字串.问如果将m个加号插入到这个数字串中,在各种可能形成的表达式中,值最小的那个表达式的值是多少 解题思路 假定数字串长度是n,添完加号后,表达式的最后一个加号 ...

  10. 51nod1624 取余最长路 前缀和 + set

    由于只有3行,因此只会会换行2次,假设$x, y$分别为这两次的换行点 那么答案为$S[1][x] +S[2][y] - S[2][x - 1] + S[3][n] - S[3][y - 1]$ 其中 ...