当前位置:网站首页>LeetCode每日一题(858. Mirror Reflection)
LeetCode每日一题(858. Mirror Reflection)
2022-08-04 11:56:00 【wangjun861205】
There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
The square room has walls of length p and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
Given the two integers p and q, return the number of the receptor that the ray meets first.
The test cases are guaranteed so that the ray will meet a receptor eventually.
Example 1:
Input: p = 2, q = 1
Output: 2
Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
Example 2:
Input: p = 3, q = 1
Output: 1
Constraints:
- 1 <= q <= p <= 1000
把房间想象成一个电梯, 电梯的所有 m * p(m 为大于 0 的整数)的高度处左右两边都挂着传感器, 奇数层挂 2 和 1, 偶数层挂 2 和 0,然后这个题就变成了求最小公倍数的题
impl Solution {
pub fn mirror_reflection(p: i32, q: i32) -> i32 {
let mut m = 1;
let mut is_up = true;
loop {
if p * m % q == 0 {
if p * m / q % 2 == 0 {
return 2;
}
if is_up {
return 1;
}
return 0;
}
m += 1;
is_up = !is_up;
}
}
}
边栏推荐
- UMA & Hong Kong Polytechnic & Ali propose SP-ViT to learn 2D space prior knowledge for visual Transformer!
- 拦截器,文件流,下载文件?
- TPC藏宝计划IDO自由协议复利模式开发功能分析
- ECCV 2022 | Towards Data Efficient Transformer Object Detectors
- A Survey of Multi-Label Classification under Supervised and Semi-Supervised Learning
- 树莓派入门
- SchedulX V1.5.0发布,提供快速压测、对象存储等全新功能!
- The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test
- MTBF是什么意思?交换机做MTBF有什么要求?MTTF、MTBF和MTTR的区别是什么?
- Leetcode Brush Questions - Path Sum
猜你喜欢
随机推荐
深度学习------pytorch实现cifar10数据集
电源测试之输出动态响应(Output Dynamic Response Test)
小程序实战(一)- 骨架屏的应用与实现
剑指长城炮? 长安全新皮卡官方谍照
如何过一个充满科技感的七夕?华为告诉你
考研数一数二数三之间的具体详细区别
喂,你知道节流是什么吗?
网管型交换机比傻瓜交换机多了哪些功能
Leetcode刷题——路径总和
OAuth2图文快速入门
Move the blog to CSDN
Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
防抖函数封装
AI 助力双碳目标:让每一度电都是我们优化的
你值得拥有的登录注册页面(附赠源码)
博云入选 Gartner 中国 DevOps 代表厂商
Disc burning steps
微信服务号调用API实现微信报警
树莓派入门
请 AI 画家弄了个 logo,网友热议:画得非常好,下次别画了!