数学
一杯小熊
这个作者很懒,什么都没留下…
展开
-
剑指Offer 43 1~n整数中1出现的次数
class Solution: def countDigitOne(self, n: int) -> int: digit, res = 1, 0 high, cur, low = n // 10, n % 10, 0 while high or cur: if cur == 0: res += high*digit elif cur == 1: res += high*digit + low...原创 2021-03-22 10:33:24 · 37 阅读 · 0 评论 -
剑指Offer 16 数值的整数次方
class Solution: def myPow(self, x: float, n: int) -> float: if x == 0: return 0 if n < 0: x, n = 1/x, -n res = 1 while n: if n & 1 == 1: res *= x # n % 2 == 1 x *= x n &...原创 2021-03-15 14:47:11 · 65 阅读 · 0 评论 -
剑指Offer 56- 数组中数字出现的次数
class Solution: def singleNumbers(self, nums: List[int]) -> List[int]: x, y, z, m = 0, 0, 0, 1 for n in nums: z ^= n while z & m == 0: m <<= 1 for n in nums: if n & m: x^= n ...原创 2021-03-10 21:46:23 · 55 阅读 · 0 评论 -
剑指Offer 10 斐波那契数列
def fib(self, n: int) -> int: a, b = 0, 1 for _ in range(n): a, b = b, a + b return a % (int(1e9+7))原创 2021-03-09 23:22:20 · 80 阅读 · 0 评论 -
剑指Offer 49 丑数
class Solution: def nthUglyNumber(self, n: int) -> int: res = [1] i2, i3, i5 = 0, 0, 0 for i in range(n-1): tmp = min(res[i2]*2, res[i3]*3, res[i5]*5) res.append(tmp) if tmp % 2 == 0: ...原创 2021-03-08 10:36:25 · 50 阅读 · 0 评论 -
剑指Offer 29:顺时针打印矩阵
class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix: return [] l, r, u, d, res = 0, len(matrix[0])-1, 0, len(matrix)-1, [] while True: for i in range(l, r+1): ...原创 2021-02-28 22:30:09 · 87 阅读 · 0 评论 -
LeetCode.No.29 两数相除
class Solution: def divide(self, dividend: int, divisor: int) -> int: MAX_INT = 2**31 - 1 MIN_INT = -2**31 #涉及符号问题的,用flag记录最后的符号 flag = 1 if dividend < 0: dividend, flag = -dividend, -flag...原创 2021-01-23 21:10:04 · 90 阅读 · 0 评论 -
LeetCode几个数学问题,类似脑筋急转弯
1. No.172 阶乘后的零class Solution: def trailingZeroes(self, n: int) -> int: count = 0 for i in range(5, n+1, 5): tmp = i while tmp % 5 == 0: count+=1 tmp //= 5 return c原创 2021-01-11 11:40:34 · 108 阅读 · 0 评论