成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 382. Linked List Random Node

崔曉明 / 815人閱讀

Problem

Given a singly linked list, return a random node"s value from the linked list. Each node must have the same probability of being chosen.

Follow up:
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?

Example:

// Init a singly linked list [1,2,3].
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
Solution solution = new Solution(head);

// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
solution.getRandom();

Solution
class Solution {

    /** @param head The linked list"s head.
        Note that the head is guaranteed to be not null, so it contains at least one node. */
    ListNode head;
    Random random;
    public Solution(ListNode head) {
        this.head = head;
        random = new Random();
    }
    
    /** Returns a random node"s value. */
    public int getRandom() {
        ListNode cur = head;
        int res = cur.val, count = 1;
        while (cur.next != null) {
            cur = cur.next;
            if (random.nextInt(count+1) == count) {
                res = cur.val;
            }
            count++;
        }
        return res;
    }
}

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

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72335.html

相關(guān)文章

  • leetcode382. Linked List Random Node

    摘要:題目要求要求從單鏈表中,隨機返回一個節(jié)點的值,要求每個節(jié)點被選中的概率是相等的。假如一共有個物品,需要從其中挑選出個物品,要求確保個物品中每個物品都能夠被等概率選中。對于這種等概率問題,簡答的做法是通過隨機數(shù)獲取選中物品的下標(biāo)。 題目要求 Given a singly linked list, return a random nodes value from the linked li...

    xiaodao 評論0 收藏0
  • LeetCode[138] Copy List with Random Pointer

    LeetCode[138] Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of t...

    novo 評論0 收藏0
  • [LintCode/LeetCode] Copy List with Random Pointer

    摘要:大體意思就是,先復(fù)制到,順便將所有的放在再復(fù)制所有的到,順便將所有的放在最后令,令,將和分離,返回的頭結(jié)點 Problem A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. ...

    Jacendfeng 評論0 收藏0
  • [Leetcode] Copy List with Random Pointer 復(fù)制隨機指針

    摘要:棧迭代復(fù)雜度時間空間如果不算新鏈表的空間則是思路由于隨機指針有可能產(chǎn)生環(huán)路,我們不能直接沿著隨機指針的方向一個一個復(fù)制。同時我們又不能沿著指針直接復(fù)制,因為我們不知道隨機指針?biāo)赶虻男鹿?jié)點是哪個。 Copy List with Random Pointer A linked list is given such that each node contains an additiona...

    Olivia 評論0 收藏0
  • LeetCode 138:復(fù)制帶隨機指針的鏈表 Copy List with Random Poin

    摘要:給定一個鏈表,每個節(jié)點包含一個額外增加的隨機指針,該指針可以指向鏈表中的任何節(jié)點或空節(jié)點。要求返回這個鏈表的深拷貝。提示你必須返回給定頭的拷貝作為對克隆列表的引用。確定隨機節(jié)點的關(guān)系之后再拆分鏈表。其時間復(fù)雜度為,空間復(fù)雜度為。 給定一個鏈表,每個節(jié)點包含一個額外增加的隨機指針,該指針可以指向鏈表中的任何節(jié)點或空節(jié)點。 要求返回這個鏈表的深拷貝。 A linked list is g...

    entner 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<