白菜刷LeetCode记-384. Shuffle an Array

今天早上是一道中等难度的题目,考的是洗牌算法。

个人对洗牌算法还是比较不熟悉的,因此是看答案的。参考链接为:https://www.jianshu.com/p/44100741cef5

基本思路为:

1) 将第一个元素与 n 个元素中的任意一个交换;

2) 将第二个与 n - 1 个元素进行交换;

3) 重复上述步骤,直到剩下1个元素。

代码如下:

 1 var original;
 2 var copy;
 3 var num;
 4 /**
 5  * @param {number[]} nums
 6  */
 7 var Solution = function(nums) {
 8     original = nums;
 9     copy = original.concat();
10     num = original.length;
11 };
12
13
14 /**
15  * Resets the array to its original configuration and return it.
16  * @return {number[]}
17  */
18 Solution.prototype.reset = function() {
19     return original;
20 };
21
22 /**
23  * Returns a random shuffling of the array.
24  * @return {number[]}
25  */
26 Solution.prototype.shuffle = function() {
27     let n = num;
28     while(n>1){
29         n--;
30         let k = Math.floor(Math.random()*(n+1));
31         let value = copy[k];
32         copy[k] = copy[n];
33         copy[n] = value;
34     }
35
36     return copy;
37 };
38
39 /**
40  * Your Solution object will be instantiated and called as such:
41  * var obj = Object.create(Solution).createNew(nums)
42  * var param_1 = obj.reset()
43  * var param_2 = obj.shuffle()
44  */

END

原文地址:https://www.cnblogs.com/sssysukww/p/9639708.html

时间: 2024-08-25 12:59:18

白菜刷LeetCode记-384. Shuffle an Array的相关文章

白菜刷LeetCode记-811.Subdomain Visit Count

好久没有写LeetCode,所以说坚持真的是一件很难的事情啊.今日重新开始吧,先来一道简单的题目,如下: 这道题首先想到的还是使用Map,代码如下: /** * @param {string[]} cpdomains * @return {string[]} */ var subdomainVisits = function(cpdomains) { let tmp = new Map(); let res = new Array(); for(let i = 0 ; i < cpdomains

白菜刷LeetCode记-46. Permutations

今天这一题也是中等难度,题目如下: 这一题是要实现数组的全排列.这一题是要使用遍历以及递归的思想去实现,代码如下: 1 /** 2 * @param {number[]} nums 3 * @return {number[][]} 4 */ 5 var permute = function(nums) { 6 var res = new Array(); 7 8 helper(nums, 0, nums.length - 1 , res); 9 10 return res; 11 }; 12 1

白菜刷LeetCode记-350. Intersection of Two Arrays II

今天题目如下: 比较简单,代码如下: /** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function(nums1, nums2) { let myMap = new Map(); let myArr = new Array(); nums1.forEach(function(item){ if(myMap.has(item)){ myMap.set(i

白菜刷LeetCode记-122. Best Time to Buy and Sell Stock II

今天题目如下: 要求出最大的利益.这题个人不太想得通,看了答案也不太知道为什么这样能获得最大值.代码如下: 1 /** 2 * @param {number[]} prices 3 * @return {number} 4 */ 5 var maxProfit = function(prices) { 6 let maxp = 0; 7 for(let i = 0 ; i < prices.length ; i++){ 8 if(prices[i-1] < prices[i]){ 9 maxp

白菜刷LeetCode记-328. Odd Even Linked List

发现简单题越来越少了,想偷懒都不可以了,今天的题目是中等难度的题目,题目如下: 这个题目是要根据链表的位置来修改链表,位置为奇数的节点全部排到前面,位置为偶数的节点全部排到奇数的后面,并且保持顺序不变. 想到的解决步骤为: 1.遍历数组,奇数的位置的节点组成一条新链表,偶数位置的节点组成另一个新链表: 2.将偶数链表接在奇数链表后面. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4

Java [Leetcode 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 equal

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 lik

LC 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 lik

384. Shuffle and Array(java,数组全排列,然后随机取)

题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机返回数组的一个排列, 并且使得获得数组每一个排列的概率都是相同的.为此,可以在初始化时,求出数组的所有排列.在使用shuffle方法时,随机返回全排列中的一个. 代码: public class Solution { //存储数组的所有排列 List<int[]> list = new Array