1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
mirror-reflection.py 970 Bytes
Copy Edit Raw Blame History
Allen Liu authored 7 years ago . add complexity
# Time: O(1)
# Space: O(1)
class Solution(object):
def mirrorReflection(self, p, q):
"""
:type p: int
:type q: int
:rtype: int
"""
# explanation commented in the following solution
return 2 if (p & -p) > (q & -q) else 0 if (p & -p) < (q & -q) else 1
# Time: O(log(max(p, q))) = O(1) due to 32-bit integer
# Space: O(1)
class Solution2(object):
def mirrorReflection(self, p, q):
"""
:type p: int
:type q: int
:rtype: int
"""
def gcd(a, b):
while b:
a, b = b, a % b
return a
lcm = p*q // gcd(p, q)
# let a = lcm / p, b = lcm / q
if lcm // p % 2 == 1:
if lcm // q % 2 == 1:
return 1 # a is odd, b is odd <=> (p & -p) == (q & -q)
return 2 # a is odd, b is even <=> (p & -p) > (q & -q)
return 0 # a is even, b is odd <=> (p & -p) < (q & -q)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

Search