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();
- //https://discuss.leetcode.com/topic/54022/c-solution-with-fisher-yates-algorithm/6
- class Solution {
- vector<int> arr, idx;
- public:
- Solution(vector<int> nums) {
- srand(time(NULL));
- arr.resize(nums.size());
- idx.resize(nums.size());
- ;i<nums.size();i++){
- arr[i] = nums[i];
- idx[i] = nums[i];
- }
- }
- /** Resets the array to its original configuration and return it. */
- vector<int> reset() {
- ;i<arr.size();i++)
- arr[i] = idx[i];
- return arr;
- }
- /** Returns a random shuffling of the array. */
- vector<int> shuffle() {
- int i,j;
- ; i > ; i--) {
- j = rand() % (i + );
- swap(arr[i], arr[j]);
- }
- return arr;
- }
- };
//C
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- typedef struct
- {
- int size;
- int *BaseNum;
- int *RandNum;
- }Solution;
- Solution* solutionCreate(int* nums, int size)
- {
- Solution* solu;
- solu = (Solution*)malloc(sizeof (Solution));
- solu->size = size;
- solu->BaseNum = (int *)malloc(sizeof(int)*size);
- solu->RandNum = (int *)malloc(sizeof(int)*size);
- memcpy(solu->BaseNum, nums, sizeof(int)*size);
- memcpy(solu->RandNum, nums, sizeof(int)*size);
- solu->size = size;
- return solu;
- }
- int* solutionReset(Solution* obj,int *returnSize) {
- return obj->BaseNum;
- }
- int* solutionShuffle(Solution* obj,int *returnSize) {
- int i,j,k;
- int temp[obj->size];
- srand((unsigned int)time(NULL));
- j = ;
- while (j < obj->size)
- {
- temp[j] = rand()%obj->size;
- ;i<j;i++)
- {
- if (temp[i] == temp[j]||temp[j] == obj->size)
- break;
- }
- if (i<j)
- continue;
- j++;
- }
- ;k < obj->size;k++)
- {
- //printf ("%d\t",temp[k]);
- obj->RandNum[k] = obj->BaseNum[temp[k]];
- }
- return obj->RandNum;
- }
- void solutionFree(Solution* obj) {
- if(obj->BaseNum != NULL){
- free(obj->BaseNum);
- obj->BaseNum = NULL;
- }
- if(obj->RandNum != NULL){
- free(obj->RandNum);
- obj->RandNum = NULL;
- }
- obj->size = ;
- if(obj != NULL){
- free(obj);
- obj = NULL;
- }
- }
- /**
- * Your Solution struct will be instantiated and called as such:
- * struct Solution* obj = solutionCreate(nums, size);
- * int* param_1 = solutionReset(obj);
- * int* param_2 = solutionShuffle(obj);
- * solutionFree(obj);
- */
- int main(void) {
- //freopen("../tmp", "r", stdin);
- Solution *st;
- ];
- int size;
- scanf("%d", &size);
- //getchar();
- ; i < size; ++i) {
- scanf("%d", &nums[i]);
- }
- st = solutionCreate(nums, size);
- int *p1 = solutionReset(st, &size);
- printf("the orignal:");
- ; i < size; ++i) {
- printf("%d ", *p1);
- p1++;
- }
- printf("\n");
- int *p2 = solutionShuffle(st, &size);
- printf("after shuffle:");
- ; i < size; ++i) {
- printf("%d ", *p2);
- p2++;
- }
- printf("\n");
- if(st != NULL){
- solutionFree(st);
- }
- ;
- }
384. Shuffle an Array的更多相关文章
- leetcode 384. Shuffle an Array
384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...
- 384. Shuffle an Array数组洗牌
[抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- LC 384. 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- 384. Shuffle an Array(java,数组全排列,然后随机取)
题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- git commit --amend
任何时候,你都有可能需要撤消刚才所做的某些操作.接下来,我们会介绍一些基本的撤消操作相关的命令.请注意,有些撤销操作是不可逆的,所以请务必谨慎小心,一旦失误,就有可能丢失部分工作成果. 有时候我们提交 ...
- [codeforces 317]A. Perfect Pair
[codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...
- css 3d 动画 相关
transform-style: preserve-3d; 设置3D模式 perspective:700px :属性定义 3D 元素距视图的距离,以像素计.该属性允许您改变 3D 元素查看 3D 元素 ...
- [转载]JavaEE学习篇之——JQuery技术详解
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/32102187 1.简介2.工具3.jQuery对象 1.DOM对象转化成j ...
- 25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment
25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment 25 BasicUsageEnvironment0基本使用环境基类— ...
- GIT文件的三种状态
对于任何一个文件,在 Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged).已提交表示该文件已经被安全地保存在本地数据库 中了:已修改表示修改了某 ...
- 傅里叶变换库FFTW的安装配置(VS2010)
FFTW是用来计算一维或者多维的离散傅里叶变换,输入可以为实数序列也可以为复数序列的C语言的子函数库,FFTW是免费软件,是作为fft函数库的各种应用的上佳选择. 1. 从网站http://www.f ...
- 【Eclipse】eclipse che 协作开发
http://www.eclipse.org/che/ http://blog.csdn.net/ccfeng2008/article/details/50881024 http://www.osch ...
- POJ 2769
http://poj.org/problem?id=2796 题意:求n个数的和乘以这n个数中的最小值的积最大的数,以及其范围. 思路:求每一个数两边的比其大的数的和,再乘以这个数.还有一个范围,用单 ...
- Debian上安装Apache+Django全过程
-->start sudo apt-get install apache2 libapache2-mod-wsgi #https://wiki.debian.org/zh_CN/Apache s ...