当前位置:网站首页>LeetCode-384. Scramble array
LeetCode-384. Scramble array
2022-06-11 16:59:00 【Border wanderer】
Give you an array of integers nums , Design algorithms to scramble an array without repeating elements .
Realization Solution class:
Solution(int[] nums) Using integer arrays nums Initialize object
int[] reset() Reset the array to its initial state and return
int[] shuffle() Returns the result of randomly scrambling the array
Example :
Input
["Solution", "shuffle", "reset", "shuffle"]
[[[1, 2, 3]], [], [], []]
Output
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
explain
Solution solution = new Solution([1, 2, 3]);
solution.shuffle(); // Scramble the array [1,2,3] And return the result . whatever [1,2,3] The probability of return should be the same . for example , return [3, 1, 2]
solution.reset(); // Reset the array to its initial state [1, 2, 3] . return [1, 2, 3]
solution.shuffle(); // Random return array [1, 2, 3] The result of the disruption . for example , return [1, 3, 2]
Tips :
1 <= nums.length <= 200
-106 <= nums[i] <= 106
nums All the elements in are Unique
You can call at most 5 * 104 Time reset and 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;
};边栏推荐
- solr(二)solr添加core以及依赖包路径
- WYSIWYG markdown editor: typora
- 二级造价工程师值得考吗?发展前景如何?
- How to store tree structure in database
- 2022G1工业锅炉司炉考题及模拟考试
- Le compte de titres de l'école de commerce kainiu est - il sécurisé? Frais d'ouverture de compte
- Katalon Studio Enterprise
- jsp页面初始加载方式
- 2022熔化焊接与热切割上岗证题目及模拟考试
- Elasitcsearch basic learning notes (1)
猜你喜欢

Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)

485天,我远程办公的 21 条心得分享|社区征文

DFS和BFS笔记(一)基于C语言的广度优先搜索
Persistence mechanism of redis

Using MATLAB and dcraw to process digital camera raw files

2022年危险化学品经营单位主要负责人考试模拟100题及模拟考试
![[opencvsharp] spot detection barcode decoding image operation image rotation / flip / Zoom perspective transformation image display control demo notes](/img/a7/5b66ef0360ed41441653a413cd9e53.png)
[opencvsharp] spot detection barcode decoding image operation image rotation / flip / Zoom perspective transformation image display control demo notes

Why does chip design also need "Craftsmanship"?

AXI协议基础知识

RSP: An Empirical Study of remote sensing pre training
随机推荐
Leetcode 450. Deleting a node in a binary search tree
CS0006 C# 未能找到元数据文件“C:\Users\...问题
LeetCode——42. Connected to rainwater (double pointer)
A journey of database full SQL analysis and audit system performance optimization
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
Redis --- 学习 NoSQL 五大类型
^32执行上下文栈面试题
ShellBrowser . NET Crack
jsp页面初始加载方式
LeetCode——24. Exchange the nodes in the linked list in pairs (three pointers)
Global and Chinese molten carbonate fuel cell industry outlook and market panoramic Research Report 2022-2028
Analysis report on future development trend and investment suggestions of global and Chinese soybean protein industry 2022-2028
Analysis report on sales status and supply and demand prospects of phosphoric acid fuel cell industry in the world and China 2022-2028 Edition
虚拟局域网划分与虚拟局域网间路由(VLAN)
AXI协议基础知识
从0到1了解Prometheus
美团获得小样本学习榜单FewCLUE第一!Prompt Learning+自训练实战
Weekly recommended short video: rookie CEO talks about the new logistics track in the future
How to store tree structure in database
2022g1 industrial boiler stoker test questions and simulation test