成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 384. Shuffle an Array

Joyven / 1450人閱讀

Problem

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

Solution Note

Random random = new Random();

manipulate on a copy of given array, so that reset() would work;

int j = random.nextInt(i+1); then swap A[i] and A[j].

class Solution {

    private int[] N;
    private Random random;
    public Solution(int[] nums) {
        this.N = nums;
        random = new Random();
    }
    
    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return N;
    }
    
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        if (N == null) return null;
        int[] A = N.clone();
        for (int i = 0; i < A.length; i++) {
            int j = random.nextInt(i+1);
            swap(A, i, j);
        }
        return A;
    }
    
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/72224.html

相關(guān)文章

  • leetcode384. Shuffle an Array

    摘要:題目要求實(shí)現(xiàn)和方法,分別能夠完成數(shù)組的隨機(jī)打亂和還原。隨機(jī)打亂即該數(shù)組中元素的所有排列組合結(jié)果都能夠以等比例的概率輸出。下面解釋一下證明,即為何每個(gè)該結(jié)果是等概率的排列組合結(jié)果。 題目要求 Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[...

    cooxer 評論0 收藏0
  • [LeetCode] Shuffle an Array

    Problem 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 ...

    Baaaan 評論0 收藏0
  • GTAV智能駕駛源碼詳解(二)——Train the AlexNet

    摘要:智能駕駛源碼詳解二模型簡介本使用進(jìn)行圖像分類前進(jìn)左轉(zhuǎn)右轉(zhuǎn)。其性能超群,在年圖像識別比賽上展露頭角,是當(dāng)時(shí)的冠軍,由團(tuán)隊(duì)開發(fā),領(lǐng)頭人物為教父。 GTAV智能駕駛源碼詳解(二)——Train the AlexNet 模型簡介: 本AI(ScooterV2)使用AlexNet進(jìn)行圖像分類(前進(jìn)、左轉(zhuǎn)、右轉(zhuǎn))。Alexnet是一個(gè)經(jīng)典的卷積神經(jīng)網(wǎng)絡(luò),有5個(gè)卷積層,其后為3個(gè)全連接層,最后的輸出...

    jayzou 評論0 收藏0
  • 也談前端面試常見問題之『數(shù)組亂序』

    摘要:看完部分的源碼,首先迫不及待想跟大家分享的正是本文主題數(shù)組亂序。這是一道經(jīng)典的前端面試題,給你一個(gè)數(shù)組,將其打亂,返回新的數(shù)組,即為數(shù)組亂序,也稱為洗牌問題。關(guān)于數(shù)組亂序,正確的解法應(yīng)該是,復(fù)雜度。 前言 終于可以開始 Collection Functions 部分了。 可能有的童鞋是第一次看樓主的系列文章,這里再做下簡單的介紹。樓主在閱讀 underscore.js 源碼的時(shí)候,學(xué)到...

    tracy 評論0 收藏0
  • JDK Collections.shuffle(List<?> list, Random

    摘要:的源碼如下一首先是判斷要打亂的的屬性的和是否實(shí)現(xiàn)接口如果的小于或者實(shí)現(xiàn)了接口,則直接交換內(nèi)元素的位置。以上內(nèi)容如有不正確的地方,歡迎支持。 jdk的源碼如下 public static void shuffle(List list, Random rnd) { int size = list.size(); if (size < SHUFFLE_THRE...

    Aomine 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<