Shuffle a set of numbers without duplicates.

Example:

  1. // Init an array with set 1, 2, and 3.
  2. int[] nums = {1,2,3};
  3. Solution solution = new Solution(nums);
  4.  
  5. // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
  6. solution.shuffle();
  7.  
  8. // Resets the array back to its original configuration [1,2,3].
  9. solution.reset();
  10.  
  11. // Returns the random shuffling of array [1,2,3].
  12. solution.shuffle();

  1. //https://discuss.leetcode.com/topic/54022/c-solution-with-fisher-yates-algorithm/6
  2. class Solution {
  3. vector<int> arr, idx;
  4. public:
  5. Solution(vector<int> nums) {
  6. srand(time(NULL));
  7. arr.resize(nums.size());
  8. idx.resize(nums.size());
  9. ;i<nums.size();i++){
  10. arr[i] = nums[i];
  11. idx[i] = nums[i];
  12. }
  13. }
  14.  
  15. /** Resets the array to its original configuration and return it. */
  16. vector<int> reset() {
  17. ;i<arr.size();i++)
  18. arr[i] = idx[i];
  19. return arr;
  20. }
  21.  
  22. /** Returns a random shuffling of the array. */
  23. vector<int> shuffle() {
  24. int i,j;
  25. ; i > ; i--) {
  26. j = rand() % (i + );
  27. swap(arr[i], arr[j]);
  28. }
  29. return arr;
  30. }
  31. };

//C

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. typedef struct
  6. {
  7. int size;
  8. int *BaseNum;
  9. int *RandNum;
  10. }Solution;
  11.  
  12. Solution* solutionCreate(int* nums, int size)
  13. {
  14. Solution* solu;
  15.  
  16. solu = (Solution*)malloc(sizeof (Solution));
  17. solu->size = size;
  18. solu->BaseNum = (int *)malloc(sizeof(int)*size);
  19. solu->RandNum = (int *)malloc(sizeof(int)*size);
  20. memcpy(solu->BaseNum, nums, sizeof(int)*size);
  21. memcpy(solu->RandNum, nums, sizeof(int)*size);
  22. solu->size = size;
  23. return solu;
  24. }
  25.  
  26. int* solutionReset(Solution* obj,int *returnSize) {
  27. return obj->BaseNum;
  28. }
  29.  
  30. int* solutionShuffle(Solution* obj,int *returnSize) {
  31. int i,j,k;
  32. int temp[obj->size];
  33. srand((unsigned int)time(NULL));
  34. j = ;
  35. while (j < obj->size)
  36. {
  37. temp[j] = rand()%obj->size;
  38. ;i<j;i++)
  39. {
  40. if (temp[i] == temp[j]||temp[j] == obj->size)
  41. break;
  42. }
  43. if (i<j)
  44. continue;
  45. j++;
  46. }
  47. ;k < obj->size;k++)
  48. {
  49. //printf ("%d\t",temp[k]);
  50. obj->RandNum[k] = obj->BaseNum[temp[k]];
  51. }
  52. return obj->RandNum;
  53. }
  54.  
  55. void solutionFree(Solution* obj) {
  56. if(obj->BaseNum != NULL){
  57. free(obj->BaseNum);
  58. obj->BaseNum = NULL;
  59. }
  60. if(obj->RandNum != NULL){
  61. free(obj->RandNum);
  62. obj->RandNum = NULL;
  63. }
  64. obj->size = ;
  65. if(obj != NULL){
  66. free(obj);
  67. obj = NULL;
  68. }
  69. }
  70.  
  71. /**
  72. * Your Solution struct will be instantiated and called as such:
  73. * struct Solution* obj = solutionCreate(nums, size);
  74. * int* param_1 = solutionReset(obj);
  75. * int* param_2 = solutionShuffle(obj);
  76. * solutionFree(obj);
  77. */
  78.  
  79. int main(void) {
  80. //freopen("../tmp", "r", stdin);
  81. Solution *st;
  82. ];
  83. int size;
  84. scanf("%d", &size);
  85. //getchar();
  86. ; i < size; ++i) {
  87. scanf("%d", &nums[i]);
  88. }
  89. st = solutionCreate(nums, size);
  90. int *p1 = solutionReset(st, &size);
  91. printf("the orignal:");
  92. ; i < size; ++i) {
  93. printf("%d ", *p1);
  94. p1++;
  95. }
  96. printf("\n");
  97. int *p2 = solutionShuffle(st, &size);
  98. printf("after shuffle:");
  99. ; i < size; ++i) {
  100. printf("%d ", *p2);
  101. p2++;
  102. }
  103. printf("\n");
  104.  
  105. if(st != NULL){
  106. solutionFree(st);
  107. }
  108. ;
  109. }

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 commit --amend

    任何时候,你都有可能需要撤消刚才所做的某些操作.接下来,我们会介绍一些基本的撤消操作相关的命令.请注意,有些撤销操作是不可逆的,所以请务必谨慎小心,一旦失误,就有可能丢失部分工作成果. 有时候我们提交 ...

  2. [codeforces 317]A. Perfect Pair

    [codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...

  3. css 3d 动画 相关

    transform-style: preserve-3d; 设置3D模式 perspective:700px :属性定义 3D 元素距视图的距离,以像素计.该属性允许您改变 3D 元素查看 3D 元素 ...

  4. [转载]JavaEE学习篇之——JQuery技术详解

    原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/32102187 1.简介2.工具3.jQuery对象 1.DOM对象转化成j ...

  5. 25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment

    25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment 25 BasicUsageEnvironment0基本使用环境基类— ...

  6. GIT文件的三种状态

    对于任何一个文件,在 Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged).已提交表示该文件已经被安全地保存在本地数据库 中了:已修改表示修改了某 ...

  7. 傅里叶变换库FFTW的安装配置(VS2010)

    FFTW是用来计算一维或者多维的离散傅里叶变换,输入可以为实数序列也可以为复数序列的C语言的子函数库,FFTW是免费软件,是作为fft函数库的各种应用的上佳选择. 1. 从网站http://www.f ...

  8. 【Eclipse】eclipse che 协作开发

    http://www.eclipse.org/che/ http://blog.csdn.net/ccfeng2008/article/details/50881024 http://www.osch ...

  9. POJ 2769

    http://poj.org/problem?id=2796 题意:求n个数的和乘以这n个数中的最小值的积最大的数,以及其范围. 思路:求每一个数两边的比其大的数的和,再乘以这个数.还有一个范围,用单 ...

  10. Debian上安装Apache+Django全过程

    -->start sudo apt-get install apache2 libapache2-mod-wsgi #https://wiki.debian.org/zh_CN/Apache s ...