• 382. 链表随机节点


     

    labuladong 题解思路
    难度中等

    给你一个单链表,随机选择链表的一个节点,并返回相应的节点值。每个节点 被选中的概率一样 。

    实现 Solution 类:

    • Solution(ListNode head) 使用整数数组初始化对象。
    • int getRandom() 从链表中随机选择一个节点并返回该节点的值。链表中所有节点被选中的概率相等。

    示例:

    输入
    ["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
    [[[1, 2, 3]], [], [], [], [], []]
    输出
    [null, 1, 3, 2, 2, 3]
    
    解释
    Solution solution = new Solution([1, 2, 3]);
    solution.getRandom(); // 返回 1
    solution.getRandom(); // 返回 3
    solution.getRandom(); // 返回 2
    solution.getRandom(); // 返回 2
    solution.getRandom(); // 返回 3
    // getRandom() 方法应随机返回 1、2、3中的一个,每个元素被返回的概率相等。



    先说结论,当你遇到第 i 个元素时,应该有 1/i 的概率选择该元素,1 - 1/i 的概率保持原有的选择。看代码容易理解这个思路:

     

     



    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode() : val(0), next(nullptr) {}
     *     ListNode(int x) : val(x), next(nullptr) {}
     *     ListNode(int x, ListNode *next) : val(x), next(next) {}
     * };
     */
    class Solution {
    public:
        ListNode* head;
        Solution(ListNode* head) {
            this->head = head;
        }
        
        int getRandom() {
            int i = 1;
            int res = 0;
            for (ListNode* node=head; node!=nullptr;node=node->next) {
                if (rand()%i==0) {
                    res = node->val;
                }
                i++;
            }
            return res;
        }
    };
    
    /**
     * Your Solution object will be instantiated and called as such:
     * Solution* obj = new Solution(head);
     * int param_1 = obj->getRandom();
     */




  • 相关阅读:
    jmeter中生成不重复的序列
    jmeter返回解密
    jmeter请求加密
    查看链接设备和安装软件
    jmeter_事物控制器_Transaction Controller
    jmeter返回中提取匹配多个参数($1$$2$)
    jmeter官方文档地址
    DEDECMS会员注册如何配置邮箱发送邮件功能
    颤抖吧,Css3
    html embed用法
  • 原文地址:https://www.cnblogs.com/zle1992/p/16622618.html
Copyright © 2020-2023  润新知