我平时打篮球,喜欢用很简单的招数,但是依然能够胜利,因此编程, 我也用最简单的方法。
1:案例一,加1
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
#讲列表转换为字符串
l=''
for i in digits:
l=l.__add__(str(i))
#将列表转换为整形并加1
m=int(l)
m+=1
#将整形转换为字符串,再讲字符串添加到列表
s=[]
for i in str(m):
s.append(int(i))
return s
案例二:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
#找出最小的值
cost=float('+inf')
#盈利的最恶劣情况就是不盈利
profice=0
for price in prices:
#每次比对找出最小的值
cost=min(cost,price)
#每次比对找出盈利的最大可能性
profice=max(profice,price-cost)
return profice
案例三
链表在C语言中练习的比较多,之前在工作中遇到过链表使用一级指针导致出错的情况。在Python中反而练习的比较少
案例四
class Solution:
def isStrobogrammatic(self, num):
dic = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}
tmp = ""
for i in num:
if i not in dic:
return False
tmp += dic[i]
return tmp[::-1] == num