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 ...
随机推荐
- Eclipse的link方式安装JBPM6插件(JBPM学习之一)
1. 首先下载最新的JAVA开发最受欢迎的Eclipse IDE工具,下载地址:http://www.eclipse.org/downloads/ 2. 然后去JBPM社区去下载最新的JBPM6,下载 ...
- MongoDB的学习和使用(MongoDB GridFS)
MongoDB GridFS GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频.视频等). GridFS 也是文件存储的一种方式,但是它是存储在MonoDB的集合中 ...
- iOS开发——多线程篇——NSOperation(基于GCD多线程编程),下载图片并合成新图片
一.NSOperation的基本概念1.简介NSOperation的作用配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperatio ...
- python 函数式编程工具
有三个内置函数与列表一起使用时非常有用:filter().map()和reduce(). 1. filter(function, sequence)返回的序列由function(item)结果为真的元 ...
- phpcms采集地址中为相对路径解决方法
1.修改数据库v9_collection_node,增加两个字段replace_from,replace_to(varchar(200)) 2./phpcms/modules/collection/t ...
- POJ 1458 1159
http://poj.org/problem?id=1458 一道容易的DP,求最长公共子序列的 dp[i][j],代表str1中的第i个字母和str2中的第j个字母的最多的公共字母数 #includ ...
- Python 开发轻量级爬虫03
Python 开发轻量级爬虫 (imooc总结03--简单的爬虫架构) 现在来看一下一个简单的爬虫架构. 要实现一个简单的爬虫,有哪些方面需要考虑呢? 首先需要一个爬虫调度端,来启动爬虫.停止爬虫.监 ...
- Effective C++ -----条款33:避免遮掩继承而来的名称
derived classes内的名称会遮掩base classes内的名称.在public继承下从来没有人希望如此. 为了让被遮掩的名称再见天日,可使用using声明式或转交函数(forwardin ...
- manage account
#!/bin/bash # #Delete_user - Automates the steps to remove an account # ############################ ...
- 【编程题目】如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
转自:http://blog.csdn.net/vast_sea/article/details/8167968 看上去似乎任何已知的算法都无法做到,如果谁做到了,那么所有的排序方法:QuickSor ...