题66(简单):
python代码:
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
s_str=''.join([str(i) for i in digits])
n=str(int(s_str)+1)
n_str=list(n)
res=[int(i) for i in n_str]
return res
题67(简单):
python代码:
class Solution:
def addBinary(self, a: str, b: str) -> str:
a_to_i=int(a,2)
b_to_i=int(b,2)
res_10=a_to_i+b_to_i
res_2=bin(res_10)
res=str(res_2)[2:]
return res
题68(困难):
分析:
感觉这题也不配困难啊,思路清晰点都随便做
python代码:
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
res=[]
tmp_line=[]
now_len=0
#遍历一遍,确定每一行塞哪些单词
for index,value in enumerate(words):
#首先判断是否可以塞下这个单词
#now_len+=单词长度+1
if now_len+len(value)<=maxWidth:
#如果可以塞下这个单词,那么直接塞进去
now_len+=len(value)+1
tmp_line.append(value)
else:
#如果塞不下
kg_num=maxWidth-now_len+1
i=0
while kg_num>0:
#从第一个的后面加空格
tmp_line[i]+=' '
kg_num-=1
i+=1
if i>=len(tmp_line)-1:
i=0
line=' '.join(tmp_line)
res.append(line)
tmp_line=[value]
now_len=len(value)+1
#如果是最后一个单词
if index==len(words)-1:
line=' '.join(tmp_line)+' '*(maxWidth-now_len+1)
res.append(line)
return res
题69(简单):
python代码:
class Solution:
def mySqrt(self, x: int) -> int:
for i in range(1,x+2):
if i*i>x:
return i-1
return 0
题70(简单):
python代码:
class Solution:
def climbStairs(self, n: int) -> int:
#使用动态数组
auto_list=[1 for i in range(n+1)]
for i in range(1,n+1):
if i==1:
auto_list[i]=1
continue
auto_list[i]=auto_list[i-1]+auto_list[i-2]
return auto_list[-1]