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. python 内置函数 : compile()

    这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. 参数source是一串字符串的源码,或者是 ...

  2. #Deep Learning回顾#之基于深度学习的目标检测(阅读小结)

    原文链接:https://www.52ml.net/20287.html 这篇博文主要讲了深度学习在目标检测中的发展. 博文首先介绍了传统的目标检测算法过程: 传统的目标检测一般使用滑动窗口的框架,主 ...

  3. Caffe学习系列(8):solver及其配置

    solver是caffe的核心. net: "examples/mnist/lenet_train_test.prototxt" test_iter: 100 test_inter ...

  4. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  5. BZOJ 3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦

    Description 给你每个点与相邻点的距离和方向,求两点间的曼哈顿距离. \(n \leqslant 4\times 10^4\) . Sol 加权并查集. 像向量合成一样合并就可以了,找 \( ...

  6. 一个按比特位拷贝数据的函数copybits

    一个按比特位拷贝数据的函数 没有进行特别的优化.其实还可以在拷贝源开始位置和目标开始位置是2的整数倍位置的时候进行优化. 说明 这个函数用于从src数组首地址跳过sbb个字节,又跳过ssb个比特位,拷 ...

  7. Android俄罗斯方块AI设计文档

    首先上源码: https://github.com/bingghost/SimpleTetris 一.概要 使用了2种AI算法: 一种是经典的Pierre Dellacherie算法 一种基于基于深度 ...

  8. MFCC可视化

    大多数文章和博客介绍都是MFCC的算法流程,物理意义,这里仅仅从数据分布可视化的角度,清晰 观察MFCC特征在空间中的分布情况,加深理解. MFCC处理流程: MFCC参数的提取包括以下几个步骤: 1 ...

  9. 【Ansible】SSH Error: ssh_exchange_identification: Connection closed by remote host

    ansible ssh到目标机器 时好时坏,报错:  SSH Error: ssh_exchange_identification: Connection closed by remote host ...

  10. [20160701]DevideByZeroWithoutNoException——from 《Java How To Program (Early Objects), 10th》

    //一段优美的例子 import java.util.Scanner; import java.util.InputMismatchException; public class DevideByZe ...