在寫代碼的路上積少成多,現在都是最笨的方法實現要求,一點一點改進。
1. 替換空格
請實現一個函數,將一個字符串中的每個空格替換成“%20”。例如,當字符串爲We Are Happy.則經過替換之後的字符串爲We%20Are%20Happy。
分析:
①使用replace()
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return s.replace(' ', '%20')
②使用split()和join()
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return '%20'.join(s.split(' ') )
str = Solution()
string = 'We are a team'
string = str.replaceSpace(string)
print string
2.從頭到尾打印鏈表
輸入一個鏈表,按鏈表值從尾到頭的順序返回一個ArrayList。
分析
主要使用的函數是:list.reverse()
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 返回從尾部到頭部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
m = []
head = listNode
while(head):
m.append(head.val)
head = head.next
m.reverse()
return m
3.二維數組查找
在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
分析:
會忽略的條件是列表爲空:[],[[]],特別是第二種情況,列表len([[]])=1的時候,還需要再判斷len(array[0])是不是也是空的。
# -*- coding:utf-8 -*-
class Solution:
# array 二維列表
def Find(self, target, array):
# write code here
count = 0
if array is not None:
for i in range(len(array)):
if len(array[0]) ==0:
return False
else:
for j in range(len(array[1])):
if target == array[i][j]:
count += 1
else:
return False
if count > 0:
return True
else:
return False
4.重建二叉樹
輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。
分析:
遞歸思想,將先序和中序結合現將二叉樹的左右節點分開,然後再將左右作爲一個樹進行分解。
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 返回構造的TreeNode根節點
def reConstructBinaryTree(self, pre, tin):
# write code here
#先序遍歷:遍歷順序規則爲【根左右】
#中序遍歷:遍歷順序規則爲【左根右】
#後序遍歷:遍歷順序規則爲【左右根】
if pre == []:
return None
val = pre[0]
ind = tin.index(val)
ltin = tin[0:ind]
rtin = tin[ind+1:]
lpre = pre[1:len(ltin)+1]
rpre = pre[len(ltin)+1:]
root = TreeNode(val)
root.left = self.reConstructBinaryTree(lpre, ltin)
root.right = self.reConstructBinaryTree(rpre, rtin)
return root
a = [1,2,4,7,3,5,6,8]
b = [4,7,2,1,5,3,8,6]
ll = Solution()
c = ll.reConstructBinaryTree(a, b)
[]和None是不一樣的。
pre = []
if pre is None:
print (True)
else:
print(False)
**False**
if pre == []:
print (True)
else:
print(False)
**True**
5.斐波那契數列
大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項爲0)。
n<=39
分析:
①笨辦法循環:
# -*- coding:utf-8 -*-
class Solution:
def Fibonacci(self, n):
# write code here
ret = []
ret.append(0)
ret.append(1)
ret.append(1)
for i in range(3, n+1):
ret.append(ret[i-1]+ ret[i-2])
return ret[n]
6.跳臺階
一隻青蛙一次可以跳上1級臺階,也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法(先後次序不同算不同的結果)。
分析:
假設一次跳一個臺階,那麼剩下的n-1個臺階就有f(n-1)種方法。
假設一次跳兩個臺階,那麼剩下的n-2個臺階就有f(n-1)種方法。
所以,n個臺階的跳法就有f(n-1)+f(n-2)種方法,是遞歸,所以可以參考菲波那切數列方法。
# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
ret = []
ret.append(0)
ret.append(1)
ret.append(2)
for i in range(3, number+1):
ret.append(ret[i-1]+ret[i-2])
return ret[number]
7.變態跳臺階
一隻青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的臺階總共有多少種跳法。
分析:
可以跳一級,二級,三級,…到n級,那麼跳上n級臺階可以記爲:
f(n) = f(n-1) + f(n-2) + f(n-3) + … + f(1) + f(0)
又
f(n-1) = f(n-2) + f(n-3) + f(n-3) + … + f(1) + f(0)
所以
f(n) = f(n-1) + f(n-1) = 2 * f(n-1)
# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
ret = []
ret.append(0)
ret.append(1)
for i in range(2, number+1):
ret.append(2*ret[i-1])
return ret[number]
8.旋轉數組的最小數字
把一個數組最開始的若干個元素搬到數組的末尾,我們稱之爲數組的旋轉。 輸入一個非減排序的數組的一個旋轉,輸出旋轉數組的最小元素。 例如數組{3,4,5,1,2}爲{1,2,3,4,5}的一個旋轉,該數組的最小值爲1。 NOTE:給出的所有元素都大於0,若數組大小爲0,請返回0。
分析:簡單的比較即可
# -*- coding:utf-8 -*-
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if len(rotateArray) == 0:
return 0
if len(rotateArray) == 1:
return rotateArray
temp = []
for i in range(len(rotateArray)-1):
if rotateArray[i] > rotateArray[i+1]:
temp = rotateArray[i+1:]
temp = temp + rotateArray[:i+1]
rotateArray = temp
return rotateArray[0]
9.求1+2+3+…+n
分析:
①利用數學公式sum=n*(n+1)/2.0
# -*- coding:utf-8 -*-
class Solution:
def Sum_Solution(self, n):
# write code here
sum = 0
sum = (1+n)*n/2.0
return sum
②遞歸
# -*- coding:utf-8 -*-
class Solution:
def Sum_Solution(self, n):
# write code here
if n == 1:
return 1
if n >= 2:
return n + self.Sum_Solution(n-1)