[LeetCode] 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();
给一个没有重复数字的数组,实现重置和洗牌的功能。
解法:遍历数组每个位置,每次都随机生成一个坐标位置,然后交换当前位置和随机位置的数字,这样如果数组有n个数字,那么也随机交换了n组位置,从而达到了洗牌的目的。
Java:
import java.util.Random;
public class Solution {
private int[] nums;
private Random random;
public Solution(int[] nums) {
this.nums = nums;
random = new Random();
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
return nums;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
if(nums == null) return null;
int[] a = nums.clone();
for(int j = 1; j < a.length; j++) {
int i = random.nextInt(j + 1);
swap(a, i, j);
}
return a;
}
private void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
Python:
import random
class Solution(object):
def __init__(self, nums):
"""
:type nums: List[int]
:type size: int
"""
self.__nums = nums
def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
return self.__nums
def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
nums = list(self.__nums)
for i in xrange(len(nums)):
j = random.randint(i, len(nums)-1)
nums[i], nums[j] = nums[j], nums[i]
return nums
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
C++:
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 < res.size(); ++i) {
int t = i + rand() % (res.size() - i);
swap(res[i], res[t]);
}
return res;
}
private:
vector<int> v;
};
All LeetCode Questions List 题目汇总
[LeetCode] 384. Shuffle an Array 数组洗牌的更多相关文章
- 384. Shuffle an Array数组洗牌
[抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- [LeetCode] 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
384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- 数组洗牌算法-shuffle
数组洗牌,最近直接的想法是从数组随机取出一个元素,放到另一个数组中,但是这样取出的元素会有重复,必须采取一定的方法保证: 1. 元素不能重复2. 元素被抽取的概率相等,即随机性 数组洗牌经典算法有两种 ...
- POJ 3087 Shuffle'm Up(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- django项目中使用手机号登录
本文使用聚合数据的短信接口,需要先获取到申请接口的appkey和模板id 项目目录下创建ubtils文件夹,定义返回随机验证码和调取短信接口的函数 function.py文件 import rando ...
- python笔记38-使用zmail发各种邮件案例代码
前言 本篇介绍使用zmail发各种格式的邮件,并运行成功的代码,小伙伴们只需更换自己的邮箱就可以运行起来了 content_text发送纯文本 先从最简单的发送纯文本的邮件开始,调通发送邮件的代码. ...
- tcp中设置连接超时
直接上代码: 设置连接超时 //首先改成非阻塞套接字 unsigned ; int rm=ioctl(sConnect,FIONBIO,(unsigned long*)&ul); ) { pr ...
- python Thread 函数
构造方法: Thread(group=None, target=None, name=None, args=(), kwargs={}) group: 线程组,目前还没有实现,库引用中提示必须是No ...
- python面试题&练习题之嵌套循环
1.打印如下结果: 1*5=5 2*10=20 3*15=45 ... 10*50=500 for i in range(1,11): print(str(i)+'x'+str((i*5))+'='+ ...
- junit4的初级用法
junit4初级用法: 一:各个标签的意思 1.@Test用来标注测试函数 2.@Before用来标注此函数在每次测试函数运行之前运行(每执行一个@Test之前都要运行一遍@Before) 3.@Af ...
- [HTML5] Using HTMLPortalElement to improve MPA preformance
For multi pages application, it is very slow to navgiate between page by page, because it needs to r ...
- 做勇敢女孩 https://www.bilibili.com/video/av14346123?from=search&seid=14078047355739050009
So a few years ago, I did something really brave, or some would say really stupid. I ran for congres ...
- 关于#pragma once和#ifndef
[1]#pragma once这个宏有什么作用? 为了避免同一个头文件被包含(include)多次,C/C++中有两种宏实现方式:一种是#ifndef方式:另一种是#pragma once方式.在能够 ...
- BZOJ 4771: 七彩树 可持久化线段树+树链的并
这个思路挺有意思的 ~ 利用树链的并来保证每个颜色只贡献一次,然后用可持久化线段树维护 code: #include <set> #include <cstdio> #incl ...