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 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();

Runtime: 244 ms, faster than 36.91% of C++ online submissions for Shuffle an Array.

class Solution {
private:
  vector<int> Nums ;
public:
    Solution(vector<int> nums) {
        Nums = nums;
    }

    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
      return Nums;
    }

    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
      vector<int> tmpnums = Nums;
      for(int i=0; i<Nums.size(); i++){
        int newidx = i + rand()%(Nums.size() - i);
        int tmp = tmpnums[i];
        tmpnums[i] = tmpnums[newidx];
        tmpnums[newidx] = tmp;
      }
      return tmpnums;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * vector<int> param_1 = obj.reset();
 * vector<int> param_2 = obj.shuffle();
 */

原文地址:https://www.cnblogs.com/ethanhong/p/10198310.html

时间: 2024-11-05 13:36:12

LC 384. Shuffle an Array的相关文章

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

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

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

白菜刷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

【Leetcode】Shuffle an Array

题目链接:https://leetcode.com/problems/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 retur

[LeetCode] 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

[Swift]LeetCode384. 打乱数组 | 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

leetcode mock Shuffle an Array

1. shuffle算法: http://www.cnblogs.com/huaping-audio/archive/2008/09/09/1287985.html 注意:我们一般用的是第二种swap的方法:但是第一种直接选择,然后把最末尾一位填上的方法,也是很好的.只是会需要两倍的空间. 2. Random nextInt(bound)参数表示 0-inclusive,bound-exclusive: [0, bound) package com.company; import java.ut

[?]*Shuffle an array

Shuffle a given array such that each position is equally likely. 算法导法里的一个算法:function randomize(A)n=A.lengthfor i=1 to nj=random(i,n)swap A[i],A[j]