popvip44的博客

class Solution {
public:
Note that the head is guaranteed to be not null, so it contains at least one node. */
ListNode* p;
}

/** Returns a random node's value. */
int getRandom() {
int res = p->val;
ListNode* node = p->next;
int i = 2;
while (node) {
int j = rand() % i;
if (j == 0)//这样写的原理是：若生成的随机数在0~i之间，则j=0时rand()=i,
//若生成的随机数在0~2i之间时，rand()=i或2i时j=0，以此类推，这样就保证了上面说明中随机数小于1/n那条性质
res = node->val;
i++;
node = node->next;
}
return res;
}
};

382. Linked List Random Node (蓄水池抽样，java)

2017-04-10 15:27:57

[leetcode] 382. Linked List Random Node 解题报告

2016-08-12 04:45:46

[leetcode] 382. Linked List Random Node

2016-08-10 16:41:06

【LeetCode】Copy List with Random Pointer 解题报告

2014-09-04 17:01:53

2016-08-23 16:18:22

LeetCode[Linked List]: Copy List with Random Pointer

2014-11-02 20:17:27

LeetCode || Copy List with Random Pointer

2014-06-24 00:17:30

LeetCode: Copy List with Random Pointer题解

2014-12-16 13:35:40

LeetCode | Copy List with Random Pointer

2016-08-02 09:31:34