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 ...
随机推荐
- servlet和http请求
1.servlet servlet是和平台无关的服务器组件,可以交互式的来浏览和修改数据,生成动态的web内容.它运行于 servlet容器中2.servlet容器 servlet容器负责servle ...
- js null和undefined
在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理. 总所周知:null == un ...
- ACM-括号匹配问题
对ACM仰慕已久,无奈今天才开始.好吧,遇到的第二个题目就把我难到了.(实话是第一个) 进入正题,下面Copy出题目: 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行输入一个数N(0 ...
- (原创)android中使用相机的两种方式
在社交类应用或扫描二维码的场合都需要用到手机上的摄像头 在程序中启用这一硬件主要有两类方法 1.发送intent启动系统自带的摄像应用 此应用的AndroidManifest中的intent-filt ...
- u-boot 2011.09 调用kernel 的流程
这段时候我总是觉得有个问题,u-boot 的存在是不是就是为了调用kernel 而存在的. 所以,粗浅的跟了一下这个流程,还有很多细节上的东西没有做好,往指正. u-boot-2011.9 调用内核代 ...
- Android实用代码模块集锦
1. 精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) 1 2 3 4 5 6 public static double getScreenPhysicalSize(Activity ctx) ...
- Delphi实现窗体内嵌其他应用程序窗体
实现原理是启动一个应用程序,通过ProcessID得到窗体句柄,然后对其设定父窗体句柄为本程序某控件句柄(本例是窗体内一个Panel的句柄),这样就达成了内嵌的效果. 本文实现的是内嵌一个记事本程序, ...
- BZOJ 1024: [SCOI2009]生日快乐
Description 将一个 \(x\times y\) 的矩形分成 \(n\) 块,让最长边:最短边 最小. Sol 搜索. \(n\) 只有 \(10\) 写一个类似于记搜的东西就好了. Cod ...
- BZOJ 1044: [HAOI2008]木棍分割
Description 求 \(n\) 根木棍长度为 \(L\) ,分成 \(m\) 份,使最长长度最短,并求出方案数. Sol 二分+DP. 二分很简单啊,然后就是方案数的求法. 状态就是 \(f[ ...
- 转: UAC 问题
打开VS2005.VS2008.VS2010工程,查看工程文件夹中的Properties文件夹下是否有app.manifest这个文件:如 没有,按如下方式创建:鼠标右击工程在菜单中选择“属性”,点击 ...