LC 384. Shuffle an Array
Shuffle a set of numbers without duplicates.
Example:
// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle(); // Resets the array back to its original configuration [1,2,3].
solution.reset(); // Returns the random shuffling of array [1,2,3].
solution.shuffle();
Runtime: 244 ms, faster than 36.91% of C++ online submissions for Shuffle an Array.
class Solution {
private:
vector<int> Nums ;
public:
Solution(vector<int> nums) {
Nums = nums;
}
/** Resets the array to its original configuration and return it. */
vector<int> reset() {
return Nums;
}
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> tmpnums = Nums;
for(int i=; i<Nums.size(); i++){
int newidx = i + rand()%(Nums.size() - i);
int tmp = tmpnums[i];
tmpnums[i] = tmpnums[newidx];
tmpnums[newidx] = tmp;
}
return tmpnums;
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* vector<int> param_1 = obj.reset();
* vector<int> param_2 = obj.shuffle();
*/
LC 384. Shuffle an Array的更多相关文章
- leetcode 384. Shuffle an Array
384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...
- 384. Shuffle an Array数组洗牌
[抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- 384. Shuffle an Array(java,数组全排列,然后随机取)
题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- 关于SAMBA的关键参考资源
https://www.samba.org/samba/docs/man/ https://devel.samba.org/ http://www.ubiqx.org/cifs/ https://ww ...
- 《python解释器源码剖析》第3章--python中的str对象
3.0 序 我们知道python中的字符串属于变长对象,当然和int也是一样,底层的结构体实例所维护的数据的长度,在对象没有定义的时候是不知道的.当然如果是python2的话,底层PyIntObjec ...
- deep_learning_初学neural network
神经网络——最易懂最清晰的一篇文章 神经网络是一门重要的机器学习技术.它是目前最为火热的研究方向--深度学习的基础.学习神经网络不仅可以让你掌握一门强大的机器学习方法,同时也可以更好地帮助你理解深度学 ...
- mybatis 动态sql 的笔记 以及标签
MyBatis常用OGNL表达式 e1 or e2 e1 and e2 e1 == e2,e1 eq e2 e1 != e2,e1 neq e2 e1 lt e2:小于 e1 lte e2:小于等于, ...
- Redis05——Redis高级运用(管道连接,发布订阅,布隆过滤器)
Redis高级运用 一.管道连接redis(一次发送多个命令,节省往返时间) 1.安装nc yum install nc -y 2.通过nc连接redis nc localhost 6379 3.通过 ...
- $q defer
实际应该是这样更好理解 把,asyncGreet换成实际的http请求SyncRequest. 如果我们多个请求那么 就是多个 promise我们要么就是链式then,要么就是$q.all 可以根据需 ...
- 从输入URL到页面加载全过程
从简单讲: 1. DNS域名解析:2. 建立TCP连接:3. 发送HTTP请求:4. 返回响应结果:5. 关闭TCP连接:6. 浏览器解析HTML:7. 浏览器布局渲染: 大家基本上都知 ...
- 最简单之安装hadoop单机版
一,hadoop下载 (前提:先安装java环境) 下载地址:http://hadoop.apache.org/releases.html(注意是binary文件,source那个是源码) 二,解压配 ...
- Linux配置python环境2,安装python
安装锁需要的依赖包 sudo apt-get -y install gcc-5 g++-5 libc6-dev make build-essential libssl-dev zlib1g-dev l ...
- Spring 初探(二) AOP 图集
Spring AOP属于第二代AOP.采用Java作为AOP的实现语言(AOL),采用动态代理机制和字节码生成技术实现. 代理设计模式 ISubject 对被访问者或者被访问资源的抽象,某些场景下不使 ...