当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢

如何过一个充满科技感的七夕?华为告诉你

光盘刻录步骤

数据库对象

Leetcode brush questions - 543. Diameter of binary trees, 617. Merging binary trees (recursive solution)

中电资讯 - 一路“标”升,喜迎Q3开门红

ESP8266-Arduino编程实例-MQ3酒精传感器驱动

Flutter使用 json_serializable 解析 JSON 最佳方案

systemdd-dev病毒杀

backbone核心详解系列——RepVGG

The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test
随机推荐
剑指offer专项突击版第19天
Nacos手摸手教学【二】Nacos注册中心
中电资讯 - 一路“标”升,喜迎Q3开门红
图像分割方法
Implementation principle of function emplace_back in vector
【目标检测】yolov2特征提取网络------Darknet19结构解析及tensorflow和pytorch实现
UMA & Hong Kong Polytechnic & Ali propose SP-ViT to learn 2D space prior knowledge for visual Transformer!
使用函数
考研概率论与数理统计(知识点梳理)
UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!
Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)
*SEO*
Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时
【目标检测】YOLOv4特征提取网络——CSPDarkNet53结构解析及PyTorch实现
Based on the BiLSTM regression forecast method
涨姿势了!原来这才是多线程正确实现方式
POJ3687Labeling Balls题解
从数学角度和编码角度解释 熵、交叉熵、KL散度
200PLC转以太网与研华webaccess modbusTCP客户端在空调机上应用配置案例
网管型交换机比傻瓜交换机多了哪些功能