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 ...
随机推荐
- Jenkins升级版本
1 Jenkins的管理界面,下载最新版本的war包 2 找到自己部署Jenkins的war包的tomcat目录,替换最新的war包,重启tomcat即可 只需要把之前的war包重命名一个名字,不要以 ...
- 05.Zabbix自动化监控
1.Zabbix自动发现(被动) 网络发现官方手册 网络发现由两个阶段组成:发现discovery和动作actions 1.单击配置->自动发现->启动默认的Local network 2 ...
- 利用openssl完成自签发证书步骤--精华版
#CentOS 7 CA目录 cd /etc/pki/CA #建立 demoCA 目录结构mkdir -p ./demoCA/{private,newcerts}touch ./demoCA/inde ...
- 剖析isinstance的实现机制
python的自省机制也是其一大彪悍的特性,对于任何一个对象,我们都可以准确的获取其类型. print(type(123)) print(type("")) print(type( ...
- 裸磁盘上ext4与xfs在线扩容,非LVM
虚拟机添加一个20G的硬盘,磁盘为sdb,分区为ext4 格式化一个5Gib的磁盘出来,用dd命令写入4G数据. 一.需求是容量为5G的磁盘,文件系统为ext4的sdb1扩容到10G. 操作步骤为 1 ...
- html2canvas-html图片合成-canvas生成图片
作用 html2canvas可以通过纯JS对浏览器端经行截屏,但截图的精确度还有待提高,部分css不可识别,所以在canvas中不能完美呈现原画面样式 支持的浏览器 Firefox 3.5+ Goog ...
- R树--理解平面思维
R树数据结构 备注:参考wiki的内容. 简介 Guttman, A.; “R-trees: a dynamic index structure for spatial searching,” ACM ...
- VMware中Red Hat Enterprise Linux 7 配置桥接模式局域网
在VMware中将虚拟机的网络连接设置为桥接模式. 在Red Hat中,找到应用程序--杂项--网络连接. 修改以太网下面的网络连接,在IPV4设置中,将方法改为“手动”,添加地址,子网掩码,网管,D ...
- es实战之查询大量数据
背景 项目中已提供海量日志数据的多维实时查询,客户提出新需求:将数据导出. 将数据导出分两步: 查询大量数据 将数据生成文件并下载 本文主要探讨第一步,在es中查询大量数据或者说查询大数据集. es支 ...
- css实现单行、多行文本超出显示省略号
前言:项目中我们经常遇到这种需求,需要对单行.多行文本超出显示为省略号.这篇文章主要总结了小编解决此问题的方法,有不足之处欢迎大家指正. 单行文本省略 .ellipsis-line { border: ...