Shuffle an Array
class Solution {
private:
vector<int> arr, idx;
public:
Solution(vector<int> nums) {
srand(time(NULL));
idx.resize(nums.size());
arr.resize(nums.size());
for(int i = ; i < nums.size(); ++i)
{
arr[i] = nums[i];
idx[i] = nums[i];
}
}
/** Resets the array to its original configuration and return it. */
vector<int> reset() {
for(int i = ; i < arr.size(); ++i)
{
arr[i] = idx[i];
}
return arr;
}
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
for(int i = arr.size() - ; i >= ; --i)
{
int j = rand() % (i + );
swap(arr[i], arr[j]);
}
return arr;
}
};
初始化种子一定要在类的构造函数里

Shuffle an Array的更多相关文章
- [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 ...
- Leetcode: 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 ...
- 384. Shuffle an Array数组洗牌
[抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- [Algorithom] Shuffle an array
Shuffling is a common process used with randomizing the order for a deck of cards. The key property ...
- LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)
LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...
- [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到最大随机 ...
随机推荐
- centOS安装openoffice
centOS安装openoffice的方法: yum install openoffice.org-writer yum install openoffice.org-calc yum install ...
- Ruby学习资源汇总
from:http://segmentfault.com/a/1190000000362058 Ruby 语言 Try Ruby: 无需在你的系统中安装.Ruby,只要通过浏览器便可立即体验 Ruby ...
- Java中String对象的不可变性
首先看一个程序 package reverse; public class Reverse { public static void main(String[] args) { String c1=n ...
- How to Send Information (String, Image, Record) Between Two Applications
http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm here are many situation when you need t ...
- 支付宝api指南
tyle="margin:20px 0px 0px; line-height:26px; font-family:Arial"> 在这些服务中,服务类型大致可以分为以下几类: ...
- Android Client and Jsp Server
1. Interestfriend Server https://github.com/eltld/Interestfriend_server https://github.com/774663576 ...
- TP复习12
四.特殊标签 1.比较标签 eq或者 equal 等于 neq 或者notequal 不等于 gt 大于 egt 大于等于 lt 小于 elt 小于等于 heq 恒等于 nheq 不恒等于 2.范围标 ...
- Eclipse中R文件不能自动生成
R文件不能自动生成主要是因为编译有错误,这时你想什么办法都是没有用的,clean, fix properties,都不是从根上解决问题. R文件主要是自动生成资源文件的id的,里边静态子类 ...
- iOS开发——实战篇Swift篇&UItableView结合网络请求,多线程,数据解析,MVC实战
UItableView结合网络请求,多线程,数据解析,MVC实战 学了这么久的swift都没有做过什么东西,今天就以自己的一个小小的联系,讲一下,怎么使用swift在实战中应用MVC,并且结合后面的高 ...
- VM参数简介
http://www.cnblogs.com/yuzhaoxin/p/4083612.html block_dump Linux 内核里提供了一个 block_dump 参数用来把 block 读写( ...