摘要: 1)获取未缺失数字的列表,逐个对比(很慢!) class Solution: def missingNumber(self, nums: List[int]) -> int: reallist = [i for i in range(len(nums)+1)] for i in reallist: 阅读全文
posted @ 2021-06-12 15:51 泊鸽 阅读(50) 评论(0) 推荐(0)
摘要: 1)栈思路 遇见左括号,则对应右括号入栈 遇右括号:①若栈不为空,则与栈顶比较,相同则continue,不同则返回False ②若栈为空,直接返回False class Solution: def isValid(self, s: str) -> bool: if len(s) % 2 == 1: 阅读全文
posted @ 2021-06-12 15:30 泊鸽 阅读(76) 评论(0) 推荐(0)
摘要: 1)普通写法,从第三行开始,计算中间元素,首位补1。空间复杂度较高 class Solution: def generate(self, numRows: int) -> List[List[int]]: if numRows == 1: return [[1]] if numRows == 2: 阅读全文
posted @ 2021-06-12 15:06 泊鸽 阅读(48) 评论(0) 推荐(0)
摘要: 1)转换二进制取不为零开始的字符串,补全反转输出。 class Solution: def reverseBits(self, n: int) -> int: t = bin(n)[2:] t = '0' * (32 - len(t)) + t return int(t[::-1],2) 2)移位, 阅读全文
posted @ 2021-06-11 15:15 泊鸽 阅读(77) 评论(0) 推荐(0)
摘要: 1)取异或,然后计算1的个数 class Solution: def hammingDistance(self, x: int, y: int) -> int: t = x^y count = 0 while t: count += 1 t = t& t-1 return count 阅读全文
posted @ 2021-06-11 14:50 泊鸽 阅读(44) 评论(0) 推荐(0)
摘要: 1)调库count class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1') 2)按位与,n&n-1等于去掉倒数的第一个1 class Solution: def hammingWeight(se 阅读全文
posted @ 2021-06-11 13:55 泊鸽 阅读(92) 评论(0) 推荐(0)
摘要: 1)倒序计算,如果结果大于标准数四倍,则超出,需要减去。空间复杂度大 class Solution: def romanToInt(self, s: str) -> int: roman_num = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, ' 阅读全文
posted @ 2021-06-10 14:50 泊鸽 阅读(82) 评论(0) 推荐(0)
摘要: 1)递归/循环,时间复杂度较高,重复计算 class Solution: def isPowerOfThree(self, n: int) -> bool: #递归 if n == 0: return False if n == 1: return True if (n / 3 )== 1 : re 阅读全文
posted @ 2021-06-10 14:34 泊鸽 阅读(70) 评论(0) 推荐(0)
摘要: 1)逐个判断,太慢了! 2)埃氏筛法,从2开始,1不是质数 class Solution: def countPrimes(self, n: int) -> int: isNumPrimes = [True] * n count = 0 for i in range(2, n): if isNumP 阅读全文
posted @ 2021-06-09 17:04 泊鸽 阅读(73) 评论(0) 推荐(0)
摘要: ?这是啥算法题。。 逐个判断写个循环即可 class Solution: def fizzBuzz(self, n: int) -> List[str]: result = [] for i in range(1,n+1): if i % 3 == 0 and i % 5 == 0 : result 阅读全文
posted @ 2021-06-09 16:32 泊鸽 阅读(30) 评论(0) 推荐(0)