当前位置:网站首页>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;
};边栏推荐
- ASP. Net education OA system source code education industry OA system source code with document
- seed-emulator下进行sql注入攻击(含sql环境配置)
- 2022G1工业锅炉司炉考题及模拟考试
- 2022 high voltage electrician special operation certificate examination question bank and online simulation examination
- Text driven for creating and editing images (with source code)
- ^32执行上下文栈面试题
- Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
- API management artifact that allows you to call wow directly
- select into from 和 insert into select 区别
- 启牛推荐开通的股票账户安全吗?靠谱吗
猜你喜欢

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

Message queue push / pull mode Learning & ActiveMQ and JMS learning

基于文本驱动用于创建和编辑图像(附源代码)

^32执行上下文栈面试题

LeetCode——42. Connected to rainwater (double pointer)

Center detection of circular marking points for opencv camera calibration

Learning C language from scratch day 039

2022g1 industrial boiler stoker test questions and simulation test

R1 Quick Open Pressure Vessel Operation test Library and Simulation Test in 2022

^32 execution context stack interview questions
随机推荐
Research Report on operation mode and investment opportunities of China's aluminum industry 2022-2028
Message queue push / pull mode Learning & ActiveMQ and JMS learning
^32 execution context stack interview questions
LeetCode——24. Exchange the nodes in the linked list in pairs (three pointers)
C语言:使用.h和.c文件遇到的问题总结
Composition of JVM
Wechat applet timestamp conversion time format + time subtraction
Opencv相机标定之圆形标识点中心检测
做跨境电商卖什么产品好?热销类目有哪些?
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
LeetCode——42. 接雨水(双指针)
Oracle数据库合并行记录,WMSYS.WM_CONCAT 函数的用和MySQL 中GROUP_CONCAT(id)的使用及比较。
微服务连接云端Sentinel 控制台失败及连接成功后出现链路空白问题(已解决)
网络流媒体协议的联系与区别(RTP RTCP RTSP RTMP HLS)
Oracle generates non duplicate string sys_ Guid() and MySQL generate unique values
启牛推荐开通的股票账户安全吗?靠谱吗
AXI协议基础知识
Persistence mechanism of redis
【clickhouse专栏】新建库、用户及角色
每周推荐短视频:菜鸟CEO谈未来物流新赛道