384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组。
示例:
// 以数字集合 1, 2 和 3 初始化数组。
int[] nums = {1,2,3};
Solution solution = new Solution(nums);
// 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。
solution.shuffle();
// 重设数组到它的初始状态[1,2,3]。
solution.reset();
详见:https://leetcode.com/problems/shuffle-an-array/description/
C++:
class Solution {
public:
Solution(vector<int> nums) {
vec=nums;
}
/** Resets the array to its original configuration and return it. */
vector<int> reset() {
return vec;
}
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> res=vec;
for(int i=0;i<vec.size();++i)
{
int t=i+rand()%(vec.size()-i);
swap(res[i],res[t]);
}
return res;
}
private:
vector<int> vec;
};
/**
* 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();
*/
参考:https://www.cnblogs.com/grandyang/p/5783392.html
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(java,数组全排列,然后随机取)
题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...
- [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 洗牌 水塘抽样 日 ...
- Java [Leetcode 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 ...
- LC 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 【实践】用 js 封装java shuffle函数(打乱数组下标方法)
此方法返回的会是一个全新的数组 所以并不会像java里的shuffle函数一样返回一个引用一样的数组 思路如下: 1.新建一个函数传入需要打乱下标的数组 2.获取数组的长度 3.新建一个用来保存并且返 ...
随机推荐
- struct init
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
- HDU——2647 Reward
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 学习日常笔记<day09>Http协议
1 Http协议入门 1.1 什么是http协议 http协议: 对浏览器客户端 和 服务器端 之间数据传输的格式规范 1.2 查看http协议的工具 1)使用火狐的firebug插件(右键-> ...
- spring面试相关点
刚刚开通博客,因为最近在进行各种面试,遇到各种面试问题,用这个机会整理几篇文章方便日后需要 springmvc 和springboot spring boot只是一个配置工具,整合工具,辅助工具. s ...
- cogs 66. [HAOI2004模拟] 数列问题
66. [HAOI2004模拟] 数列问题 ★☆ 输入文件:dfs3.in 输出文件:dfs3.out 简单对比时间限制:1 s 内存限制:128 MB 问题描述试编程将 1 至 N ...
- JSP的文件上传
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/file-uploading.html: 一个JSP可以用一个HTML表单标签,它允许用户上传文件到服务器 ...
- 赵雅智_Android的getResources()资源引用
今天做一个Android的刮刮乐项目.里面用到非常多的地方用到了getResources. <span style="font-size:12px;"> // 获得图片 ...
- RAD 极速应用开发 Spring ROO 入门样例
官网 http://projects.spring.io/spring-roo/ Spring ROO in action ...
- 【剑指Offer学习】【面试题65:滑动窗体的最大值】
题目:给定一个数组和滑动窗体的大小,请找出全部滑动窗体里的最大值. 举例说明 比如,假设输入数组{2,3,4,2,6,2,5,1}及滑动窗体的大小.那么一共存在6个滑动窗体,它们的最大值分别为{4,4 ...
- Xamarin iOS开发实战第1章使用C#编写第一个iOS应用程序
Xamarin iOS开发实战第1章使用C#编写第一个iOS应用程序 C#原本是用来编写Windows以及Windows Phone的应用程序.自从Xamarin问世后.C#的作用就发生了非常大的变化 ...