"Leetcode" Shuffle an Array

Source: Internet
Author: User
Tags shuffle

Title Link: https://leetcode.com/problems/shuffle-an-array/

Topic:

Shuffle a set of numbers without duplicates.

Example:

Init an array with set 1, 2, and 3.int[] Nums = {n/a}; Solution solution = new solution (nums);//Shuffle the array [] and return its result. Any permutation of [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 ();

Subscribe to see which companies asked this question

Ideas:

Using the data structure implemented in http://blog.csdn.net/yeqiuzs/article/details/52141124, each time shuffle gets the initial set of a random number and deletes it until the collection is empty, once the shuffle is completed, After completion, you need to restore the data structure of the storage collection. Time complexity Shuffle method O (n), reset method O (n), construction method O (N).

Algorithm:

public class Solution {Randomizedset rm;int nums[]; Public solution (int[] nums) {rm = new Randomizedset (); this.nums = nums;for (int i=0;i<nums.length;i++) {Rm.inse    RT (Nums[i]);} }/** resets the array to its original configuration and return it. */public int[] Reset () {rm = new Randomizedset (); for (int i=0;i<nums.length;i++) {Rm.insert (nums[i]);} return nums;} /** Returns A random shuffling of the array. */public int[] Shuffle () {randomizedset tmp = new Randomizedset (); int[] res = new Int[rm.vals.size ()];for (int i = 0; I &l T Res.length; i++) {Res[i] = Rm.getrandom (); Rm.remove (Res[i]); Tmp.insert (Res[i]);} RM = Tmp;return Res;} Class Randomizedset {/** Initialize your data structure here. */public Randomizedset () {}list<integer> vals = new Ar Raylist<integer> (); Map<integer, integer> val2idx = new Hashmap<integer, integer> ();/** * Inserts a value to the set. Returns true if the set did not already * contain the specified element. */public BoOlean Insert (int val) {if (Val2idx.containskey (val)) {return false;} else {Val2idx.put (val, Val2idx.size ()); Vals.add ( val); return true;}} /** * Removes a value from the set. Returns true if the set contained the * specified element. */public boolean remove (int val) {if (!val2idx.containskey (val)) {return false;} else {int idx = Val2idx.remove (val); if (i DX < Vals.size ()-1) {//delete the last element of the list//swap the end element and the deleted element Vals.set (idx, Vals.get (Vals.size ()-1)); Val2idx.put (Vals.get ( Vals.size ()-1), IDX);} Vals.remove (Vals.size ()-1); return true;}} /** Get a random element from the set. */random r = new Random ();p ublic int getrandom () {return Vals.get (R.nextint (Vals.size ()));}}


"Leetcode" Shuffle an Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.