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 ...
随机推荐
- Java 7 Concurrency Cookbook 翻译 第一章 线程管理之三
五.睡眠和唤醒一个线程 有时,你会想要在一段特定的时间后再去中断线程的运行.举个例子,程序中的一个线程每一分钟检查一次传感器的状态,剩余的时间,线程应该处于空闲的状态.在这段空闲时间里,线程不会使用计 ...
- android socket编程用Bufferreader读取的一个失败教训
由于我的手机需要用笔记本开的wifi,躺在床上玩手机时需要关电脑或者是要让电脑放歌的时候总是不想下床,于是我想能不能用一个APP,然后通过局域网实现在手机上对电脑进行操控呢?说干就干. 我在电脑上用的 ...
- CSU 1113 Updating a Dictionary(map容器应用)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...
- 对象的constructor属性
对象的constructor属性, 最初是用来标识对象类型的. 比如 ,我们定义一个 Person类,然后实例化两个对象. function Person(name, age, job){this.n ...
- 说说JSON和JSONP,也许你会豁然开朗
前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...
- 剑指Offer 链表中倒数第k个结点
题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路: 法1:设置2个指针p,q.p先移动k次,然后pq同时后移,p到链表尾尾的时候,q指向倒数第k个节点. 注意://需要考虑k=0,以 ...
- centos 终端 字体颜色
默认情况下,没有颜色. https://www.centos.org/docs/2/rhl-gsg-en-7.2/ls-color.html git也默认没有颜色,破解如下: git config - ...
- python 模块之间的变量共享
才疏学浅,只知道两种方式: 1. 通过__builtin__实现: builtin1.py import __builtin__ __builtin__.some_global_var_among_m ...
- DIV的摇晃效果---jquery实现
DIV的摇晃效果---jquery实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- 使用SharePoint 2010 母版页
SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...