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 ...
随机推荐
- JavaScript 中 OnLoad事件用法总结
还差一天现在手头上的这套网站就写完了,中午蹭了半天还是没睡好,干脆爬起来把今天上午写到的onload事件给整理一下. 一般用到比较多的就是初始化数据或者效果. 1.直接写在<body>标签 ...
- C++面向对象的编程思想机器人
C++的面向对象的编程思想如下,一般情况为一个类中包含了这个对象的所有属性与函数,直接调用这个对象就可以对这个对象执行它可以使用的任何操作. #include <iostream> cla ...
- HTTP请求报头及其处理
ps:详细说明http://www.cnblogs.com/kkgreen/archive/2011/04/11/2012829.html
- MySQL 根据身份证查找年龄段
SELECT idcard,YEAR (NOW()) - substring(idcard, 7, 4) as nFROM es_members where idcard >0 HA ...
- scrapy模拟浏览器爬取验证码页面
使用selenium模块爬取验证码页面,selenium模块需要另外安装这里不讲环境的配置,我有一篇博客有专门讲ubuntn下安装和配置模拟浏览器的开发 spider的代码 # -*- coding: ...
- python: find the index of a given value in a list
["foo", "bar", "baz"].index("bar")
- python 读取 xlsx
>>> xl = pd.ExcelFile("dummydata.xlsx") >>> xl.sheet_names [u'Sheet1', u ...
- CentOS下编译安装LNMP环境
一.卸载系统预安装的LAMP软件 rpm -qa|grep httpd rpm -e httpd httpd-tools rpm -qa|grep mysql rpm -e mysql mysql-l ...
- c# String.Split数组省去空字符
public static void test() { string txt = "hi[#b1.jpg][#b4.jpg]few[#b1-3.jpg]"; txt = " ...
- lbypmall虚拟主机的设置
虚拟机配置不完整,导致访问是样式路径不正确,问题可能是config.inc.php配置不正确 1.修改/etc/php.ini 访问目录限制 open_basedir =/home/upload/:/ ...