384. Shuffle an Array [Leetcode]
来源:互联网 发布:javascript官网 编辑:程序博客网 时间:2024/06/06 19:17
利用swap+随机数生成一个随机的序列。
class Solution {public: Solution(vector<int> nums) { srand(time(NULL)); this->originalNums = nums; size = nums.size(); } /** Resets the array to its original configuration and return it. */ vector<int> reset() { return originalNums; } /** Returns a random shuffling of the array. */ vector<int> shuffle() { vector<int> ShuffleNums(originalNums); for (int i = 0; i < size; i++) { swap(ShuffleNums[i], ShuffleNums[i + rand() % (size - i)]); } return ShuffleNums; }private: vector<int> originalNums; int size;};/** * 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(); */
阅读全文
0 0
- [leetcode] 384. Shuffle an Array
- 【leetcode】384. Shuffle an Array
- 384.[LeetCode]Shuffle an Array
- [LeetCode]384. Shuffle an Array
- [LeetCode] 384. Shuffle an Array
- LeetCode 384. Shuffle an Array
- LeetCode 384. Shuffle an Array
- LeetCode 384. Shuffle an Array
- [Leetcode]384. Shuffle an Array
- 【LeetCode】 384. Shuffle an Array
- [leetcode]384. Shuffle an Array
- LeetCode 384. Shuffle an Array
- [LeetCode]384. Shuffle an Array
- [LeetCode]384. Shuffle an Array
- leetcode 384. Shuffle an Array
- 384. Shuffle an Array [Leetcode]
- LeetCode:384. Shuffle an Array
- 【Leetcode】Shuffle an Array
- 如何实现摄像的微信预约?乐享其约
- 关于ScrollView和UIcollectionView手势冲突的问题
- NOIP2016[换教室] 期望概率DP
- js模块化开发一
- 推荐个朋友的博客
- 384. Shuffle an Array [Leetcode]
- 直播弹幕过滤敏感词之DFA算法
- Tag File文件的理解
- sqoop java.lang.NullPointerException 和 No columns to generate for ClassWriter
- ClobToString
- phoenix全局索引和本地索引测试,和hive连接
- mysql经纬度 某半径长度 内查询数据
- Premake 生成 Makefile 的缺省配置
- ios 蓝牙对战实现