Random r; int[][] rects; public Solution(int[][] rects) { r=new Random(); this.rects=rects; } public int[] pick() { //水塘抽样 int idx = -1, cur = 0, pre = 0, n = rects.length; for(int i = 0; i < n; ++i) { int x1 = rects[i][0], y1 = rects[i][1], x2 = rects[i][2], y2 = rects[i][3]; cur = (x2-x1+1) * (y2-y1+1); pre += cur; if(r.nextInt(pre)< cur)//选中当前的概率为cur/pre,保留之前的概率为1-cur/pre idx = i; } //在选中的方块随机取点 int x1 = rects[idx][0], y1 = rects[idx][1], x2 = rects[idx][2], y2 = rects[idx][3]; return new int[]{x1 + r.nextInt(x2-x1+1), y1 + r.nextInt(y2-y1+1)}; }