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 01, 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.

Return the number of the receptor that the ray meets first.  (It is guaranteed that the ray will meet a receptor eventually.)

Example 1:

  1. Input: p = 2, q = 1
  2. Output: 2
  3. Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.

Note:

  1. 1 <= p <= 1000
  2. 0 <= q <= p

Approach #1: Math. [Java]

  1. class Solution {
  2. public int mirrorReflection(int p, int q) {
  3. while (p % 2 == 0 && q % 2 == 0) {
  4. p /= 2;
  5. q /= 2;
  6. }
  7. if (p % 2 == 0) return 2;
  8. else if (q % 2 == 0) return 0;
  9. else return 1;
  10. }
  11. }

  

Analysis:

To show the mirror effect, we can....

Reference:

https://leetcode.com/problems/mirror-reflection/discuss/141765/Java-short-solution-with-a-sample-drawing

858. Mirror Reflection的更多相关文章

  1. 【LeetCode】858. Mirror Reflection 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. [LeetCode] Mirror Reflection 镜面反射

    There is a special square room with mirrors on each of the four walls.  Except for the southwest cor ...

  3. [Swift]LeetCode858. 镜面反射 | Mirror Reflection

    There is a special square room with mirrors on each of the four walls.  Except for the southwest cor ...

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  8. {ICIP2014}{收录论文列表}

    This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...

  9. Unity3D插件分享

    网上看到一个讲unity3D插件的,看着不错,转载过来. 本文汇总了近百个Unity3D插件,供大家参考下载. 2D_Toolkit_1.51 动画开发插件包 FingerGestures 触摸插件 ...

随机推荐

  1. 2021-2-27:Linux 下如何优化 Java MMAP 写入

    主要是调整 pdflush 相关参数. 在linux操作系统中,写操作是异步的,即写操作返回的时候数据并没有真正写到磁盘上,而是先写到了系统cache里,随后由pdflush内核线程将系统中的脏页写到 ...

  2. Git:版本库建立与状态查看

    版本库又名仓库,英文名repository,可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可 ...

  3. 转: 微信已支持发送最大 200MB 的视频了,并且不会被压缩 来自腾讯微信团队的视频消息,目前 iOS 版本的微信已支持发送最大 200MB 的视频与图片了,并且不会被压缩.安卓版本未来一段时间会支 ...

  4. spring 整合kafka监听消费

    前言 最近项目里有个需求,要消费kafka里的数据.之前也手动写过代码去消费kafka数据.但是转念一想.既然spring提供了消费kafka的方法.就没必要再去重复造轮子.于是尝试使用spring的 ...

  5. 使用createrepo构建本地yum仓库

    rpm包安装的时候会有很多软件会出现因为其他依赖包没有,而导致安装失败的情况.一般可以连接外网的时候我们直接使用 yum 进行安装,可以为我们解决依赖包关系,但是很多工作环境下是没有外网的,内网情况下 ...

  6. SpringMVC-05 Json交互处理

    SpringMVC-05 Json交互处理 Json 1.什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别 ...

  7. Learn Python the Hard Way,ex37-1

    本练习为复习python的符号和关键字 关键字有: #and or False True print(1==0 and 2==0, 1==0 or 2==0) print(False) print(T ...

  8. PriorityQueue 是线性结构吗?90%的人都搞错了!

    文章首发于「陈树义」公众号及个人博客 shuyi.tech 其实这个问题的完整描述是:Java 中的 PriorityQueue 实现,其数据的逻辑结构是线性结构吗?其数据的物理结构又是什么? 估计很 ...

  9. 你真的懂 i++ 和 ++i 吗?

    对于 ++i 和 i++,许多人可能都知道,不就是先加1再取值,和先取值再加1嘛.然而,真的是这样吗?请先看以下4道题,能全部答对可以忽略这篇文章. 题目 // 示例1 int i = 1; i = ...

  10. 【linux】驱动-1-环境准备

    目录 前言 1. 开发环境搭建 1.1 环境准备 1.1.1 安装工具 1.1.2 编译内核 1.1.2.1 获取内核源码 1.1.2.2 编译内核 1.2 内核驱动模块编译和加载 1.2.1 hel ...