首页 > 其他 > 详细

398. Random Pick Index

时间:2018-10-28 10:30:15      阅读:122      评论:0      收藏:0      [点我收藏+]

 

 1 class Solution {
 2     Random random = new Random();
 3     int[] arr;
 4 
 5     public Solution(int[] nums) {
 6         arr = nums;
 7     }
 8     
 9     public int pick(int target) {
10         int count = 0;
11         for(int i = 0; i < arr.length; i++){
12             if(arr[i] == target){
13                 count++;
14             }
15         }
16         int index = random.nextInt(count)+1;
17         count = 0;
18         for(int i = 0; i < arr.length; i++){
19             if(arr[i] == target){
20                 count++;
21             }
22             if(count == index){
23                 return i;
24             }
25         }
26         return -1;
27         
28         
29     }
30 }

 

398. Random Pick Index

原文:https://www.cnblogs.com/goPanama/p/9864308.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!