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 ...
随机推荐
- axios表单提交,delete,get请求(待完善)
import { mapMutations} from 'vuex' import axios from 'axios' const mixins = { data() { return { } }, ...
- Delphi TIdUDPClient组件
- spring-02
spring-02 1.谈谈你对 Spring 的理解 Spring 是一个开源框架,为简化企业级应用开发而生.Spring 可以是使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.S ...
- QTP(16)
一.QTP项目(ECShop) 1.ECShop是一个开源免费的一个B2C的电子商务系统,主要用于商家和顾客进行商品交易操作. 2.ECShop分为前台和后台两个子系统: (1)ECShop前台:顾客 ...
- Datasets and Evaluation Metrics used in Recommendation System
Movielens and Netflix remain the most-used datasets. Other datasets such as Amazon, Yelp and CiteUli ...
- JAVA 流与文件
流 InputStream和OutputStream是所有的输入流和输出流的超类.他们两个都是抽象类. read方法和write方法都是阻塞方法,这意味着如果不能里可以写入或者读取,比如因为网络问题, ...
- java 实现链表
public class MyList { Entry head; class Entry { Object data; Entry next; public Entry(Object data) { ...
- tcpip入门的网络教程汇总
网络编程懒人入门(一):快速理解网络通信协议(上篇) http://www.52im.net/thread-1095-1-1.html TCP/IP详解学习笔记 https://www.cnblogs ...
- 【转】java中的clone
java中的clone Clone&Copy 假设现在有一个Employee对象,Employee tobby =new Employee("CMTobby",5000), ...
- jsp上传超大文件解决方案
1,项目调研 因为需要研究下断点上传的问题.找了很久终于找到一个比较好的项目. 在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面. http ...