当前位置:网站首页>497. 非重叠矩形中的随机点
497. 非重叠矩形中的随机点
2022-06-09 14:10:00 【anieoo】
原题链接:497. 非重叠矩形中的随机点
solution:
① 计算所有矩形中点的数量
② 利用rand()函数随机获取一个值
③ 根据②获取的值,找到对应的矩形
④ 再从矩形中找到对应的点
class Solution {
public:
vector<vector<int>> rects;
int *area; //保存每个矩形中的点
int n;
int sum;
Solution(vector<vector<int>>& rects) {
this->rects = rects;
n = rects.size();
sum = 0;
area = (int *)malloc(sizeof(int) * n);
for(int i = 0;i < n;i++) {
area[i] = ((rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1));
sum += area[i];
}
}
vector<int> pick() {
int rnd = rand() % sum; //在所有的点中随机寻找一个点
int i,pos;
//找到点对应的矩形
for(i = 0,pos = 0;i < n;i++) {
if(pos + area[i] > rnd) break;
pos += area[i];
}
rnd -= pos;
//在找到的矩形中找到具体的点
int x = rects[i][0] + rnd % (rects[i][2] - rects[i][0] + 1);
int y = rects[i][1] + rnd / (rects[i][2] - rects[i][0] + 1);
return {x,y};
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(rects);
* vector<int> param_1 = obj->pick();
*/边栏推荐
- GreatSQL如何做中国广受欢迎的开源数据库
- Graphpad prism 9 for Mac
- 科研统计分析绘图工具GraphPad Prism 9 for Mac
- MySQL数据库的日志管理
- 文档编辑器
- Rest API中PUT 与 PATCH 使用区别及幂等性分析
- insert into select/update
- Like the brain, the nano magnet network can be used to perform AI like computing processing, which can reduce energy consumption
- [云原生]Kubernetes可视化界面WEBUI Kubernetes Dashboard
- 薪酬不變,每周只上四天班,英國試行全球最大規模“四天工作制”
猜你喜欢

I haven't published a thesis for 5 years, and I want to give up my doctoral degree? Ten thousand words self narration of the doctoral director of the University of science and technology of China: he

为什么 SQL 语句使用了索引,但却还是慢查询?

Tailwind 导致 antd 的 button icon 不居中的 bug

有奖调研

Award winning research

C# ListBox用法
![[Ji Wang] review of Cisco final multiple choice questions](/img/80/e84d8b2a5873b2d215463a503bebf7.png)
[Ji Wang] review of Cisco final multiple choice questions

Hongmeng transplantation i.mx6ull (V) overview of transplantation

鸿蒙移植i.mx6ull(六) Kconfig_GCC_Mkefile

喜报 | 旺链科技签约汨罗市文旅体产业项目,打造“链”上数字乡村
随机推荐
[tgcalls] trace the call debugging managers to the upper layer of 3:ticket:: candidate >
名片微信小程序错误版本2
Google 安装印象笔记剪藏插件
Implementing MySQL master-slave with docker compose
[Ji Wang] review of Cisco final multiple choice questions
数据分析--
U.S. restrictions on sharing security vulnerabilities will throw stones at its own feet, and domestic systems will gain development opportunities
入职腾讯外包一个月,我离职了
Hongmeng porting i.mx6ull (VIII) adding a board
《数字经济全景白皮书》银行财富管理篇 重磅发布
数据科学中的 10 个重要概念和图表的含义
In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
Award winning research
鸿蒙移植i.mx6ull(九) 串口移植(基于IMX6ULL)
文档编辑器
Code implementation WordPress writing articles can adjust font size
为什么 SQL 语句使用了索引,但却还是慢查询?
使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的“心路(累)历程“
Explain the factory method
CICC digital intelligence China II: five questions and five answers to database business market