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.新建一个用来保存并且返 ...
随机推荐
- ****Call to a member function item() on a non-object
A PHP Error was encountered Severity: Error Message: Call to a member function item() on a non-objec ...
- Test for Job 图上的动态规划(DAG)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11399 Accepted: 2697 Des ...
- [52ABP系列] - 001、SPA免费项目模版搭建教程
前言 这个项目是基于 ABP ASPNetCore 免费版,整合 NG-Alian 和 NG-Zorro 的项目,所以比较适合熟悉 ABP 和 Angular2+ 的开发人员, 如果你是新手,学习的话 ...
- GETTING STARTED WITH THE OTTO JAVASCRIPT INTERPRETER
原文: https://www.fknsrs.biz/blog/otto-getting-started.html.html GETTING STARTED WITH THE OTTO JAVASCR ...
- python中的is判断引用的对象是否一致,==判断值是否相等
python中的is判断引用的对象是否一致,==判断值是否相等 a = 10 b = 20 list = [1,2,3,4,5] print(a in list) print(b not in lis ...
- [AngularJS] Store the entry url and redirect to entry url after Logged in
For example when a outside application need to visit your app address: https://www.example.com/#/lob ...
- RelativeLayout不能居中的解决的方法
在LinearLayout中有个让元素居中的办法就是.比方在LinearLayout里有个TextView.设置TextView的gravity能够让其居中. 而在Realative里设置这个不起作用 ...
- leetcode02-Add Two Numbers之beats98.68%Java版本号
我的leetcode之旅,该篇章主要完毕使用Java实现算法. 这是第二篇Add Two Numbers 所有代码下载: Github链接:github链接,点击惊喜; 写文章不易.欢迎大家採我的文章 ...
- 【bzoj1150】[CTSC2007]数据备份Backup
将k对点两两相连,求最小长度 易证得,最优方案中,相连的办公楼一定是取相邻的比取不相邻的要更优 然后就可以用贪心来做这道题了.. 将初始所有的线段放进堆里 每次取最短的线段进行连接,且ans+=a[i ...
- sql 查询如何将结果集 输出为一段字符串?
文件id集合 文件表. SELECT CONCAT('2323',(SELECT 'dsfsd'),'232323'); SELECT CONCAT('2323',(SELECT file_ids F ...