problemscpp
A collection of my answers to algorithm problems in c++.
载入中...
搜索中...
未找到
leetcode::linked_list_random_node::Solution类 参考

#include <leetcode.h>

Public 成员函数

 Solution (ListNode *head)
 
int getRandom () const
 

Private 属性

ListNodehead
 

详细描述

在文件 leetcode.h458 行定义.

构造及析构函数说明

◆ Solution()

leetcode::linked_list_random_node::Solution::Solution ( ListNode * head)
inlineexplicit

在文件 leetcode.h463 行定义.

引用了 head.

成员函数说明

◆ getRandom()

int leetcode::linked_list_random_node::Solution::getRandom ( ) const
nodiscard

在文件 leetcode.cpp1035 行定义.

1035 {
1036 int i = 1;
1037 int ans = 0;
1038 for(const auto *node = head; node != nullptr; node = node->next, i++) {
1039 if(rand() % i == 0) {
1040 // 1/i 的概率选中(替换为答案)
1041 ans = node->val;
1042 }
1043 }
1044 return ans;
1045 }
vector< vector< int > > ans

引用了 head.

类成员变量说明

◆ head

ListNode* leetcode::linked_list_random_node::Solution::head
private

在文件 leetcode.h460 行定义.

被这些函数引用 Solution() , 以及 getRandom().


该类的文档由以下文件生成: