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的更多相关文章

  1. leetcode 384. Shuffle an Array

    384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...

  2. 384. Shuffle an Array数组洗牌

    [抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  3. Java [Leetcode 384]Shuffle an Array

    题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  4. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  5. LC 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  6. 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...

  7. 384. Shuffle an Array(java,数组全排列,然后随机取)

    题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...

  8. 384 Shuffle an Array 打乱数组

    打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...

  9. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

随机推荐

  1. git之install

    一.window安装 1.下载路径 https://git-for-windows.github.io/ 2.如何在windows下安装GIT_百度经验 3.做完上面两部打开Git bash即可执行g ...

  2. mysql常用语句总结

    1.创建语句 CREATE DATABASE database_name //创建数据库 //删表 DROP TABLE IF EXISTS `t_social_user_extend`; //建表C ...

  3. css3延时动画

    不太理解属性都是什么意思,但是有动画效果,我也是惊呆了 <style> #animated_div{animation:animated_div 4s 1; -moz-animation: ...

  4. PYTHON 自动化之路 (二)

    一.python 模块的使用 模块的使用: import os #调用 os 模块 cmd_s = os.popen("dir").read() #打开路径为结果保存为cmd_sp ...

  5. dp水题 序列问题 (9道)

    9道题.A了8道,A题看题解也没弄懂怎么维护m段子序列的,过一段时间再回来看看     dp试水 47:56:23 125:00:00   Overview Problem Status Rank ( ...

  6. dango foreign key 指定被引用模型的字段

    用 to_field pool_no = models.ForeignKey('SimCardPool', verbose_name=u'卡池编号', db_column='pool_no', to_ ...

  7. PyCharm 4.0.6 注册码

    Professional Edition版本比Free版本多了很多东西,比如 Web development,Django等等,重新下了Professional版本,虽然是只30天免费,但是到时候重装 ...

  8. serv-u启动管理控制台后提示脚本错误解决方案

             问题描述: 安装serv-u后打开管理控制台,提示“脚本错误“控制台界面无法正常显示: 解决方法: 1.在serv-u安装目录下找到”Serv-U-DefaultCertificat ...

  9. oracle 学习

    一.数据库语言部分1. SQL语言:关系数据库的标准语言2. PL/SQL:过程化语言Procedural Language3. SQL*Plus:简单的报表,操作系统接口 4. Oracle 8.0 ...

  10. 由于Windows和Linux行尾标识引起脚本无法运行的解决

    在所有的操作系统中,文本文件的结束或者换行都是有行尾符来标识的,C语言中经常使用\n作为换行,\r作为跳格TAB:实际上在计算机还没有真正出现之前,有种电传打字机的设备,每秒钟可以打印10个字符,但是 ...