当前位置:网站首页>LeetCode-384. 打乱数组
LeetCode-384. 打乱数组
2022-06-11 16:53:00 【边界流浪者】
给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。
实现 Solution class:
Solution(int[] nums) 使用整数数组 nums 初始化对象
int[] reset() 重设数组到它的初始状态并返回
int[] shuffle() 返回数组随机打乱后的结果
示例:
输入
["Solution", "shuffle", "reset", "shuffle"]
[[[1, 2, 3]], [], [], []]
输出
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
解释
Solution solution = new Solution([1, 2, 3]);
solution.shuffle(); // 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。例如,返回 [3, 1, 2]
solution.reset(); // 重设数组到它的初始状态 [1, 2, 3] 。返回 [1, 2, 3]
solution.shuffle(); // 随机返回数组 [1, 2, 3] 打乱后的结果。例如,返回 [1, 3, 2]
提示:
1 <= nums.length <= 200
-106 <= nums[i] <= 106
nums 中的所有元素都是 唯一的
最多可以调用 5 * 104 次 reset 和 shuffle
#include<iostream>
#include<vector>
#include<stdlib.h>
using namespace std;
class Solution {
public:
Solution(vector<int>& nums) {
source = nums;
nochange = nums;
}
vector<int> reset() {
return nochange;
}
vector<int> shuffle() {
vector<int> result;
vector<int>::iterator it = source.begin();
while (!source.empty()) {
int index = (rand() % (source.size()));
result.push_back(source[index]);
auto erase = source.begin() + index;
source.erase(erase);
}
source = nochange;
return result;
}
private:
vector<int> nochange;
vector<int> source;
};边栏推荐
- Composition of JVM
- Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)
- [pytest learning] after the pytest case fails to execute, the others will not be executed
- 基于文本驱动用于创建和编辑图像(附源代码)
- VLAN partition and routing between VLANs
- Connection and difference of network streaming media protocol (RTP RTCP RTMP HLS)
- ^32执行上下文栈面试题
- 2022安全员-C证特种作业证考试题库及答案
- LeetCode——24. Exchange the nodes in the linked list in pairs (three pointers)
- Kernel density estimation (2D, 3D)
猜你喜欢

solr(一)solr的安装及权限控制

多任务学习经典品读:MMoE模型篇

消息队列-推/拉模式学习 & ActiveMQ及JMS学习

Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)

Redis - learn five types of NoSQL

2022安全员-C证特种作业证考试题库及答案

【opencvsharp】opencvsharp_ samples. Core sample code Notes

Using MATLAB and dcraw to process digital camera raw files

Analysis report on sales status and supply and demand prospects of phosphoric acid fuel cell industry in the world and China 2022-2028 Edition

485天,我远程办公的 21 条心得分享|社区征文
随机推荐
VLAN partition and routing between VLANs
DFS和BFS笔记(一)基于C语言的广度优先搜索
Association relationship
The micro service failed to connect to the cloud sentinel console and the link blank problem occurred after the connection was successful (resolved)
2022 high voltage electrician special operation certificate examination question bank and online simulation examination
Classic reading of multi task learning: MMOE model
web安全-靶场笔记
Persistence mechanism of redis
(validation file) validatejarfile report errors
基于udp端口猜测的内网穿透
所见即所得的 markdown 编辑器:Typora
Elasitcsearch basic learning notes (1)
Analysis report on the "fourteenth five year plan" proposal and innovation environment of global and Chinese sodium pyrophosphate industry (2022-2028)
Simulated 100 questions and simulated examination for main principals of hazardous chemical business units in 2022
Oracle 分析函数 over 和MySQL 实现类似效果写法
Analysis report on sales status and supply and demand prospects of phosphoric acid fuel cell industry in the world and China 2022-2028 Edition
2022年安全员-B证国家题库及模拟考试
^31 prototype interview questions
2022G1工业锅炉司炉考题及模拟考试
Le compte de titres de l'école de commerce kainiu est - il sécurisé? Frais d'ouverture de compte