Shuffle a set of numbers without duplicates.

Example:

  1. // Init an array with set 1, 2, and 3.
  2. int[] nums = {1,2,3};
  3. Solution solution = new Solution(nums);
  4.  
  5. // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
  6. solution.shuffle();
  7.  
  8. // Resets the array back to its original configuration [1,2,3].
  9. solution.reset();
  10.  
  11. // Returns the random shuffling of array [1,2,3].
  12. solution.shuffle();

Runtime: 244 ms, faster than 36.91% of C++ online submissions for Shuffle an Array.

  1. class Solution {
  2. private:
  3. vector<int> Nums ;
  4. public:
  5. Solution(vector<int> nums) {
  6. Nums = nums;
  7. }
  8.  
  9. /** Resets the array to its original configuration and return it. */
  10. vector<int> reset() {
  11. return Nums;
  12. }
  13.  
  14. /** Returns a random shuffling of the array. */
  15. vector<int> shuffle() {
  16. vector<int> tmpnums = Nums;
  17. for(int i=; i<Nums.size(); i++){
  18. int newidx = i + rand()%(Nums.size() - i);
  19. int tmp = tmpnums[i];
  20. tmpnums[i] = tmpnums[newidx];
  21. tmpnums[newidx] = tmp;
  22. }
  23. return tmpnums;
  24. }
  25. };
  26.  
  27. /**
  28. * Your Solution object will be instantiated and called as such:
  29. * Solution obj = new Solution(nums);
  30. * vector<int> param_1 = obj.reset();
  31. * vector<int> param_2 = obj.shuffle();
  32. */

LC 384. Shuffle an Array的更多相关文章

  1. leetcode 384. Shuffle an Array

    384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...

  2. 384. Shuffle an Array数组洗牌

    [抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  3. 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  4. Java [Leetcode 384]Shuffle an Array

    题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  5. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  6. 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...

  7. 384. Shuffle an Array(java,数组全排列,然后随机取)

    题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...

  8. 384 Shuffle an Array 打乱数组

    打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...

  9. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

随机推荐

  1. GDI+ 绘图教程 验证码

    使用的 C# winform using System; using System.Collections.Generic; using System.ComponentModel; using Sy ...

  2. vim 去掉自动注释和自动回车

    取消 :set paste 恢复 :set paste!

  3. ubuntu安装软件apt-get

    一. apt-get用法 apt 0.8.16~exp12ubuntu10.26 for i386 compiled on Aug  5 2015 19:06:21Usage: apt-get [op ...

  4. deep_learning_Softmax()

    该节课中提到了一种叫作softmax的函数,因为之前对这个概念不了解,所以本篇就这个函数进行整理,如下: 维基给出的解释:softmax函数,也称指数归一化函数,它是一种logistic函数的归一化形 ...

  5. 1.Shell脚本

    1.Shell脚本 可以将Shell终端解释器当作人与计算机硬件之间的“翻译官”,它作为用户与Linux系统内部的通信媒介,除了能够支持各种变量与参数外,还提供了诸如循环.分支等高级编 程语言才有的控 ...

  6. imx6ull增加qt5 qtserialbus库

    meta-qt5库地址:https://code.qt.io/cgit/yocto/meta-qt5.git/   1.在fsl-release-yocto/sources/meta-qt5/reci ...

  7. shell脚本基础和grep文本处理工具企业应用3

    文本处理工具:    linux上文本处理三剑客        grep,egrep,fgrep:文本过滤工具(模式:pattern)工具            grep:默认支持的是基本正则表达式: ...

  8. 8.Canny边缘检测

    #导入工具包 from imutils import * image = imread('image/school.jpg') show(image) def edge_detection(image ...

  9. zencart新增categories分类表字段步骤

    zencart新增分类字段步骤 1.categories表新增字段related_categories.related_products ) ) NOT NULL; 2.修改admin\categor ...

  10. 关于 python 一切皆对象的实际理解

    1 关于type type 函数可以查看一个对象的类 type 类是一切类型的模版 In [2]: type(1) Out[2]: int In [3]: type(int) Out[3]: type ...