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 ...
随机推荐
- 小程序开发之改变data中数组或对象的某一属性值
前言:在小程序的开发中,我们在view中便利data中数组或对象时,很多情况下需要在js中动态改变数组或者对象中某一香的属性值. 效果图: 我给大家总结了案例如下: wxml如下: <scr ...
- 【剑指offer】和为s的两个数字
原创博文,转载请注明出处! # 题目 # 思路 首先定义两个指针,第一个指针p指向数组的第一个数字,第二个指针q指向数组的最后一个数字.如果p+q=s,则找到要找的数字:如果p+q<s,则p向后 ...
- UDP示例
android学习笔记18--------------UDP示例 分类: android2011-11-10 10:07 848人阅读 评论(0) 收藏 举报 androidbufferexcepti ...
- 滑动ViewPager引起swiperefreshlayout刷新的冲突
ViewPager是Android中提供的页面切换的控件,SwipeRefreshLayout是Android提供的下拉刷新控件,通过SwipeRefreshLayout可以很简单的实现下拉刷新的功能 ...
- C++中atof函数的实现和atoi的实现
在C++中有两个系统函数可以实现字符串转浮点型和字符串转整形,下面实现一下这两个函数. #include <iostream> #include <string> using ...
- BZOJ1345 Baltic2007 序列问题Sequence 【思维题】*
BZOJ1345 Baltic2007 序列问题Sequence Description 对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用 ...
- 在 GitHub 公开仓库中隐藏自己的私人邮箱地址
GitHub 重点在开方源代码,其本身还是非常注重隐私的.这一点与面向企业的 GitLab 很不一样. 不过,你依然可能在 GitHub 上泄露隐私信息,例如企业内部所用的电子邮箱. GitHub 对 ...
- 微信小程序(3)——常用的组件
view: view是小程序中的视图容器之一,似于html中的<div>标签 <view class="section"> <view class=& ...
- netstat 命令 与 ss 命令
http://www.cnblogs.com/peida/archive/2013/03/11/2953420.html http://www.ttlsa.com/linux-command/ss-r ...
- checkstyle简单使用说明
checkstyle对检查代码规范问题的总结,虽然还不够只能,但已经比较强大.1.Cyclomatic Complexity is X (max allowed is X). 问题说明:圈复杂度过高. ...