有了python基礎知識積累,決定再花些時間鞏固一下,這樣在後期項目開發中會更加輕松,廢話不多說,是用的刷題網站是lintCode,直接看題:
1、A+B問題:
非标準型答案:
class Solution:
"""
@param a: An integer
@param b: An integer
@return: The sum of a and b
"""
def aplusb(self, a, b):
return a+b
2、尾部的零
非标準型答案:
class Solution:
"""
@param: n: An integer
@return: An integer, denote the number of trailing zeros in n!
"""
def trailingZeros(self,n):
count = 0
sum=n
sign=1
while n>1:
sum=sum*(n-1)
n=n-1
sumStr = str(sum);
length = len(sumStr)-1
while length>=0 and sign==1:
if int(sumStr[length])==0:
count+=1
else:
sign=0
length-=1
return count
答案分析:上面的程式在lintCode上面,效率是不過關的,後期進行優化
3、統計數字
class Solution:
"""
@param k: An integer
@param n: An integer
@return: An integer denote the count of digit k in 1..n
"""
def digitCounts(self, k, n):
i = 0
count = 0
while i<=n:
tempStr = str(i);
j=0
while j
if(int(tempStr[j])==k):
count+=1
j+=1
i+=1
return count
今天就隻刷了三道題,大家也可以使用編輯器自行敲寫一遍,加深印象,後續還會繼續釋出最新刷題記錄。