Skip to content

Commit 2e15726

Browse files
authored
Update random-pick-index.cpp
1 parent 3edb470 commit 2e15726

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

C++/random-pick-index.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ class Solution {
99

1010
int pick(int target) {
1111
auto reservoir = -1;
12-
int n = 0;
12+
auto n = 0;
1313
for (int i = 0; i < nums_.size(); ++i) {
1414
if (nums_[i] != target) {
1515
continue;
1616
}
17-
if (++n == 1 || rand() % n == 0) {
17+
if (rand() % ++n == 0) {
1818
reservoir = i;
1919
}
2020
}

0 commit comments

Comments
 (0)