leetcode384
public class Solution {
private int[] nums;
private Random random;
public Solution(int[] nums)
{
this.nums = nums;
random = new Random();
}
/** Resets the array to its original configuration and return it. */
public int[] Reset()
{
return nums;
}
/** Returns a random shuffling of the array. */
public int[] Shuffle()
{
if (nums == null)
{
return null;
}
int[] a = (int[])nums.Clone();
for (int j = ; j < a.Length; j++)
{
int i = random.Next(j + );
Swap(a, i, j);
}
return a;
}
private void Swap(int[] a, int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int[] param_1 = obj.Reset();
* int[] param_2 = obj.Shuffle();
*/
https://leetcode.com/problems/shuffle-an-array/#/description
leetcode384的更多相关文章
- [Swift]LeetCode384. 打乱数组 | Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- Git钩子:自定义你的工作流
Git钩子是在Git仓库中特定事件发生时自动运行的脚本.它可以让你自定义Git内部的行为,在开发周期中的关键点触发自定义的行为. Git钩子最常见的使用场景包括推行提交规范,根据仓库状态改变项目环境, ...
- php序号发生器,数字重组,可以隐藏原来的1,2,3。。。
一个晚上的成果,原理: 将1,2,3,4,5,6,7,8,9,0映射到9,5,1,0,4,8,7,3,2,6 同理映射base,base有1-10种数组,也就是可以一位数到10位数 $base 实际上 ...
- POJ3422 Kaka's Matrix Travels 【费用流】*
POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...
- LeetCode 760. Find Anagram Mappings
原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/ 题目: Given two lists Aand B, ...
- [转]50个很棒的Python模块
转自:http://www.cnblogs.com/foxhengxing/archive/2011/07/29/2120897.html Python具有强大的扩展能力,以下列出了50个很棒的Pyt ...
- kong 了解
Kong 是在客户端和(微)服务间转发API通信的API网关,通过插件扩展功能.Kong 有两个主要组件: Kong Server :基于nginx 的服务器,用来接收 API 请求. Apac ...
- 3.Python连接数据库PyMySQL
1.安装PyMySQL,输入命令:pip3 install PyMySQL 2.使用Navicat,创建数据库:TESTDB,表:EMPLOYEE,字段:FIRST_NAME,LAST_NAME,AG ...
- Spring Cloud 服务网关Zuul
Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...
- (转)Inno Setup入门(二十二)——Inno Setup类参考(8)
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17268473 列表框 列表框(ListBox)是Windows应用程 ...
- 阻塞队列之三:SynchronousQueue同步队列 阻塞算法的3种实现
一.SynchronousQueue简介 Java 6的并发编程包中的SynchronousQueue是一个没有数据缓冲的BlockingQueue,生产者线程对其的插入操作put必须等待消费者的移除 ...