题目

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();

分析

给定一个数组nums,对其进行洗牌,要求nums中的每个元素都以等概率被移动,并且能够返回初始数组。

class Solution {
public:Solution(vector<int> nums) :v(nums){}/** Resets the array to its original configuration and return it. */vector<int> reset() {return v;}/** Returns a random shuffling of the array. */vector<int> shuffle() {vector<int> res=v;for(int i=0;i<v.size();i++){int j=rand()%v.size();swap(res[i],res[j]);}return res;}
private:vector<int> v;
};/*** 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();*/

384. Shuffle an Array相关推荐

  1. leetcode 384. Shuffle an Array | 384. 打乱数组(Fisher-Yates洗牌算法)

    题目 https://leetcode.com/problems/shuffle-an-array/ 题解 本代码参考了 JDK 源码 Collections.shuffle() 的实现,也可以看 官 ...

  2. 20190906:(leetcode习题)Shuffle an Array

    Shuffle an Array 题目 大致思路 代码实现 题目 大致思路 没有特殊的技巧,直接生成一个随机索引,将该值与序列的i个元素交换即可.i从1遍历到结束. 调用的Random对象的nextI ...

  3. 【Algorithm】 Reservoir Sampling Shuffle an Array

    Reservoir Sampling Example Question: Randomly choosing k samples from a list of n items, where n is ...

  4. 【Leetcode】Shuffle an Array

    题目链接:https://leetcode.com/problems/shuffle-an-array/ 题目: Shuffle a set of numbers without duplicates ...

  5. LeetCode Shuffle an Array(Fisher-Yates洗牌算法)

    问题:打乱一个没有重复元素的数组 思路:Fisher-Yates洗牌.首先从1到n中选取一个数,删除,然后从1到n-1中选取一个数删除,直至剩下一个元素 代码具体参考: https://github. ...

  6. 1470. Shuffle the Array

    给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列. 请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列, ...

  7. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

  8. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  9. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

最新文章

  1. PyTorch 1.9发布,支持新API,可在边缘设备中执行
  2. redis 的惊群处理和分布式锁的应用例子
  3. 数据中心进水了怎么办?数据中心如何防洪?
  4. keepAliveTime和线程工厂
  5. CF 961E Tufurama
  6. axios vue 加载效果动画_vue+axios+element ui 实现全局loading加载示例
  7. Android调用相册、相机(兼容6.0、7.0、8.0)
  8. 保存pdf文件的一种方法
  9. 音视频处理之FFmpeg封装格式20180510
  10. 【转】JMeter Tutorial的安装和具体操作
  11. Java汉字转拼音实现方式
  12. PKI密码学学习笔记
  13. 【githubshare】开源的小说下载与阅读工具:Uncle 小说。目录解析与书源结合,支持有声小说与文本小说,可下载 mobi、epub、txt 格式文本小说
  14. android实现打电话
  15. 彻底解决“你的时钟快了”问题
  16. mysql 匹配多个字符,【单选题】在MySQL语句中,可以匹配0个到多个字符的通配符是( ) A. * B. % C. ? D. –...
  17. 手机wps怎么设置打印横竖_手机WPS怎么设置横版打印?
  18. WT588E语音芯片+数码管的应用场景介绍
  19. js 拖曳功能--代码解析
  20. SpringBoot思维导图(知识点总结)

热门文章

  1. 实验六 51单片机定时/计数器实验
  2. [闪存2.1.6] NAND FLASH基本编程(写)操作及原理_NAND FLASH Program Operation 源码实现
  3. SAP WM 先进先出原则在’Stock Removal Background’的时候的体现
  4. Spark07:【案例】Transformation和Action
  5. ElasticSearch的学习
  6. 湖南安全职院•美和易思举办第一届卓越项目发布会
  7. 无损动态磁盘转基本磁盘方法 (win7 系统亲测)
  8. labview100个实例之信号采集
  9. 2022-2028年中国橱柜拉篮行业市场调查研究及发展前景规划报告
  10. Docker 拉取 oracle 11g镜像配置,以及修改服务名为orcl