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. Triangle leetcode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. 信号屏蔽的切换的理解sigsuspend

    #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h&g ...

  3. weapp微信小程序初探demo

    https://github.com/donglegend/weapp-demo 参考文档开发工具安装微信weapp API git项目源码微信小程序 demo效果展示效果预览

  4. Java Io 流(输入输出流)

    IO流,也就是输入和输出流,可分为字节流和字符流. 1. 字节流 (1). InputStream 输入流,用于读取文件 输入流常用API: inputStream.read()  读取一个字节 in ...

  5. iOS开发——UI进阶篇(十)导航控制器、微博详情页、控制器的View的生命周期

    一.导航控制器出栈 1.initWithRootViewController本质 UIViewController *vc = [[OneViewController alloc] init]; // ...

  6. Java序列化技术与Protobuff

    http://www.cnblogs.com/fangfan/p/4094175.html http://www.cnblogs.com/fangfan/p/4094175.html 前言: Java ...

  7. Windows8笔记

    一.WIN8 硬盘变为可弹出设备 如果电脑使用的是nforce芯片组的主板和串口硬盘.在系统中安装好所有的硬件驱动程序后,会出现这种情况.这是nforce芯片组在安装了IDE-SW主板驱动以后,系统会 ...

  8. CentOS6.5安装Tab增强版:bash-completion

    CentOS6.5安装Tab增强版:bash-completion,可补全命令参数: 因为CentOS官方源并不带有bash-completion的包,所以,为了可用yum安装,增加epel的源, 首 ...

  9. PHP 面向对象:抽象类继承抽象类

    抽象类继承另外一个抽象类时,不用重写其中的抽象方法.抽象类中,不能重写抽象父类的抽象方法.这样的用法,可以理解为对抽象类的扩展. 下面的例子,演示了一个抽象类继承自另外一个抽象类时,不需要重写其中的抽 ...

  10. CLR via C# 随记

    使用C# 编译器的方法: 1.csc.exe位于C:\Windows\Microsoft.NET\Framework\vxxxxx下面,将对应版本的路径配置到环境变量path中,如将";C: ...