@@ -72,22 +72,149 @@ solution.pickIndex(); // 返回 0,返回下标 0,返回该下标概率为 1/
72
72
73
73
<!-- 这里可写通用的实现逻辑 -->
74
74
75
+ “前缀和 + 二分查找”。
76
+
75
77
<!-- tabs:start -->
76
78
77
79
### ** Python3**
78
80
79
81
<!-- 这里可写当前语言的特殊实现逻辑 -->
80
82
81
83
``` python
82
-
84
+ class Solution :
85
+
86
+ def __init__ (self , w : List[int ]):
87
+ n = len (w)
88
+ self .presum = [0 ] * (n + 1 )
89
+ for i in range (n):
90
+ self .presum[i + 1 ] = self .presum[i] + w[i]
91
+
92
+ def pickIndex (self ) -> int :
93
+ n = len (self .presum)
94
+ x = random.randint(1 , self .presum[- 1 ])
95
+ left, right = 0 , n - 2
96
+ while left < right:
97
+ mid = (left + right) >> 1
98
+ if self .presum[mid + 1 ] >= x:
99
+ right = mid
100
+ else :
101
+ left = mid + 1
102
+ return left
103
+
104
+ # Your Solution object will be instantiated and called as such:
105
+ # obj = Solution(w)
106
+ # param_1 = obj.pickIndex()
83
107
```
84
108
85
109
### ** Java**
86
110
87
111
<!-- 这里可写当前语言的特殊实现逻辑 -->
88
112
89
113
``` java
114
+ class Solution {
115
+ private int [] presum;
116
+
117
+ public Solution (int [] w ) {
118
+ int n = w. length;
119
+ presum = new int [n + 1 ];
120
+ for (int i = 0 ; i < n; ++ i) {
121
+ presum[i + 1 ] = presum[i] + w[i];
122
+ }
123
+ }
124
+
125
+ public int pickIndex () {
126
+ int n = presum. length;
127
+ int x = (int ) (Math . random() * presum[n - 1 ]) + 1 ;
128
+ int left = 0 , right = n - 2 ;
129
+ while (left < right) {
130
+ int mid = (left + right) >> 1 ;
131
+ if (presum[mid + 1 ] >= x) {
132
+ right = mid;
133
+ } else {
134
+ left = mid + 1 ;
135
+ }
136
+ }
137
+ return left;
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Your Solution object will be instantiated and called as such:
143
+ * Solution obj = new Solution(w);
144
+ * int param_1 = obj.pickIndex();
145
+ */
146
+ ```
147
+
148
+ ### ** C++**
149
+
150
+ ``` cpp
151
+ class Solution {
152
+ public:
153
+ vector<int > presum;
154
+
155
+ Solution(vector<int>& w) {
156
+ int n = w.size();
157
+ presum.resize(n + 1);
158
+ for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + w[i];
159
+ }
160
+
161
+ int pickIndex () {
162
+ int n = presum.size();
163
+ int x = rand() % presum[n - 1] + 1;
164
+ int left = 0, right = n - 2;
165
+ while (left < right)
166
+ {
167
+ int mid = left + right >> 1;
168
+ if (presum[mid + 1] >= x) right = mid;
169
+ else left = mid + 1;
170
+ }
171
+ return left;
172
+ }
173
+ };
174
+
175
+ /* *
176
+ * Your Solution object will be instantiated and called as such:
177
+ * Solution* obj = new Solution(w);
178
+ * int param_1 = obj->pickIndex();
179
+ */
180
+ ```
90
181
182
+ ### ** Go**
183
+
184
+ ``` go
185
+ type Solution struct {
186
+ presum []int
187
+ }
188
+
189
+ func Constructor (w []int ) Solution {
190
+ n := len (w)
191
+ pre := make ([]int , n+1 )
192
+ for i := 0 ; i < n; i++ {
193
+ pre[i+1 ] = pre[i] + w[i]
194
+ }
195
+ return Solution{pre}
196
+ }
197
+
198
+ func (this *Solution ) PickIndex () int {
199
+ n := len (this.presum )
200
+ x := rand.Intn (this.presum [n-1 ]) + 1
201
+ left , right := 0 , n-2
202
+ for left < right {
203
+ mid := (left + right) >> 1
204
+ if this.presum [mid+1 ] >= x {
205
+ right = mid
206
+ } else {
207
+ left = mid + 1
208
+ }
209
+ }
210
+ return left
211
+ }
212
+
213
+ /* *
214
+ * Your Solution object will be instantiated and called as such:
215
+ * obj := Constructor(w);
216
+ * param_1 := obj.PickIndex();
217
+ */
91
218
```
92
219
93
220
### ** ...**
0 commit comments