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 ...
随机推荐
- GDI+ 绘图教程 验证码
使用的 C# winform using System; using System.Collections.Generic; using System.ComponentModel; using Sy ...
- vim 去掉自动注释和自动回车
取消 :set paste 恢复 :set paste!
- ubuntu安装软件apt-get
一. apt-get用法 apt 0.8.16~exp12ubuntu10.26 for i386 compiled on Aug 5 2015 19:06:21Usage: apt-get [op ...
- deep_learning_Softmax()
该节课中提到了一种叫作softmax的函数,因为之前对这个概念不了解,所以本篇就这个函数进行整理,如下: 维基给出的解释:softmax函数,也称指数归一化函数,它是一种logistic函数的归一化形 ...
- 1.Shell脚本
1.Shell脚本 可以将Shell终端解释器当作人与计算机硬件之间的“翻译官”,它作为用户与Linux系统内部的通信媒介,除了能够支持各种变量与参数外,还提供了诸如循环.分支等高级编 程语言才有的控 ...
- imx6ull增加qt5 qtserialbus库
meta-qt5库地址:https://code.qt.io/cgit/yocto/meta-qt5.git/ 1.在fsl-release-yocto/sources/meta-qt5/reci ...
- shell脚本基础和grep文本处理工具企业应用3
文本处理工具: linux上文本处理三剑客 grep,egrep,fgrep:文本过滤工具(模式:pattern)工具 grep:默认支持的是基本正则表达式: ...
- 8.Canny边缘检测
#导入工具包 from imutils import * image = imread('image/school.jpg') show(image) def edge_detection(image ...
- zencart新增categories分类表字段步骤
zencart新增分类字段步骤 1.categories表新增字段related_categories.related_products ) ) NOT NULL; 2.修改admin\categor ...
- 关于 python 一切皆对象的实际理解
1 关于type type 函数可以查看一个对象的类 type 类是一切类型的模版 In [2]: type(1) Out[2]: int In [3]: type(int) Out[3]: type ...