这题python没有官方解答,这是我自己的解法:
def toHex(self, num: int) -> str:
factors=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
s=''
if num < 0:
num = num+0x100000000
while True:
s=factors[num%16]+s
num//=16
if num==0:
break
return s

有大佬给出更棒的答案,这种运算里经常有mask,还不是很熟练。
class Solution:
def toHex(self, num: int) -> str:
num &= 0xFFFFFFFF #按位取反
s = "0123456789abcdef"
res = ""
mask = 0b1111
while num > 0:
res += s[num & mask] #num&mask提取了 num 的最低 4 个比特位的值
num >>= 4
return res[::-1] if res else "0"

415. 字符串相加
我写的方法挺复杂的,这里最好是使用双指针的形式
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
# 不能直接将输入的字符串转换为整数形式
num1=num1[::-1] #654
num2=num2[::-1] #77
l = min(len(num1),len(num2)) #2
temp=0
s=''
for i in range(l): #0,1
r=int(num1[i])+int(num2[i])+temp #13,13
s=str(r%10)+s #3,33
temp=r//10 #1,1
while l<len(num1):
r=int(num1[l])+temp #5
s=str(r%10)+s #533
temp=r//10
l+=1
while l<len(num2):
r=int(num2[l])+temp
s=str(r%10)+s
temp=r//10
l+=1
if temp:
s=str(temp)+s
return s
优解,采用双指针的形式
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
# 不能直接将输入的字符串转换为整数形式
res=''
i,j=len(num1)-1,len(num2)-1
carry=0
while i>=0 or j>=0:
n1 = int(num1[i]) if i >= 0 else 0
n2 = int(num2[j]) if j >= 0 else 0
tmp = n1 + n2 + carry
carry = tmp // 10
res = str(tmp%10)+res
i, j = i - 1, j - 1
return "1" + res if carry else res
492. 构造矩形
作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 所以,现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:
- 你设计的矩形页面必须等于给定的目标面积。
- 宽度
W不应大于长度L,换言之,要求L >= W。 - 长度
L和宽度W之间的差距应当尽可能小。
返回一个 数组 [L, W],其中 L 和 W 是你按照顺序设计的网页的长度和宽度。
我一直按照分解因子的思路来写,始终写不出来,有时候就是想得太复杂了,其实直接一边暴力遍历就好了。不用找各种因子的组合,而是整体来考虑。
class Solution:
def constructRectangle(self, area: int) -> List[int]:
w=int(sqrt(area))
while area%w:
w-=1
return [area//w,w]
509. 斐波那契数
class Solution:
def fib(self, n: int) -> int:
if n==0:
return 0
if n==1:
return 1
f=[0,1]
for i in range(2,n+1):
f.append(f[i-1]+f[i-2])
return f[-1]
法1,动态规划,减少内存:
class Solution:
def fib(self, n: int) -> int:
if n<2:
return n
a,b,c = 0,1,1
for i in range(2,n+1):
c=a+b
a,b=b,c
return c
法2,矩阵快速幂,减少时间:

刚好学习一下矩阵的运算,能学懂还是尽量学
快速计算矩阵的 n 次幂

class Solution:
def fib(self, n: int) -> int:
if n<2:
return n
q = [[1,1],[1,0]]
res = self.matrix_pow(q,n-1)
return res[0][0]
def matrix_pow(self,a:List[List[int]],n:int)->List[List[int]]:
ret=[[1,0],[0,1]]
while n>0:
if n&1:
ret = self.matrix_multiply(ret, a)
n>>=1
a = self.matrix_multiply(a, a)
return ret
def matrix_multiply(self, a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
c = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j]
return c
892. 三维形体的表面积
我觉得我有时候只是不乐意思考。
class Solution:
def surfaceArea(self, grid: List[List[int]]) -> int:
N = len(grid)
ans = 0
for r in range(N):
for c in range(N):
if grid[r][c]:
ans += 2
for nr, nc in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
if 0 <= nr < N and 0 <= nc < N:
nval = grid[nr][nc]
else:
nval = 0
ans += max(grid[r][c] - nval, 0)
return ans
908. 最小差值 I

不用实操
class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
return max(0,max(nums)-min(nums)-2*k)
1025. 除数博弈
class Solution:
def divisorGame(self, n: int) -> bool:
return n % 2 == 0
好累,不想思考
804

被折叠的 条评论
为什么被折叠?



