1.在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
# -*- coding:utf-8 -*-
#從右上角往左下角遍歷
class Solution:
# array 二維列表
def Find(self, target, array):
# write code here
if array == []:
return None
if array[0] == []:
return None
col = len(array[0])
row = len(array)
i = 0
j = col - 1
while(i<row and j >=0):
if array[i][j] == target:
return True
else:
if array[i][j] > target:
j = j-1
else:
i = i+1
return None
2.請實現一個函數,將一個字符串中的每個空格替換成“%20”。例如,當字符串爲We Are Happy.則經過替換之後的字符串爲We%20Are%20Happy。
# -*- coding:utf-8 -*-
#遍歷,遇到空格就替換
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
res = ''
for item in s:
if item == ' ':
res = res + '%20'
else:
res += item
return res
3.輸入一個鏈表,按鏈表值從尾到頭的順序返回一個ArrayList。
#可用遞歸、棧等方式
class Solution:
# 返回從尾部到頭部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
lst = []
p1 = listNode
while(p1 != None):
lst.append(p1.val)
p1 = p1.next
return lst[-1::-1]
4.輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。
class Solution:
# 返回構造的TreeNode根節點
def reConstructBinaryTree(self, pre, tin):
# write code here
if len(pre) == 0:
return None
if len(pre) == 1:
return TreeNode(pre[0])
root = TreeNode(pre[0])
root.left = self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tin[0:tin.index(pre[0])])
root.right = self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tin[tin.index(pre[0])+1:])
return root
5.把一個數組最開始的若干個元素搬到數組的末尾,我們稱之爲數組的旋轉。 輸入一個非減排序的數組的一個旋轉,輸出旋轉數組的最小元素。
# -*- coding:utf-8 -*-
#先將右側與rotateArray[0]相等的元素過濾掉,然後用二分法求解
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
n = len(rotateArray)
if n == 0:
return 0
l = 0
r = n - 1
flag = rotateArray[0]
while (n>0 and rotateArray[r] == flag):
r -= 1
if (r <=0):
return flag
while(l < r):
mid = l + (r-l)/2
if rotateArray[mid] > flag :
l = mid+1
else:
r = mid
return rotateArray[l]
6.大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項爲0)。
n<=39
# -*- coding:utf-8 -*-
class Solution:
def Fibonacci(self, n):
# write code here
n1 = 0
n2 = 1
j = 1
res = 0
if n == 0:
return n1
if n == 1:
return n2
while(j<n):
res = n1 + n2
n1 = n2
n2 = res
j += 1
return res
7.一隻青蛙一次可以跳上1級臺階,也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法(先後次序不同算不同的結果)。
# -*- coding:utf-8 -*-
#類似斐波納切數列 f(n) = f(n-1) + f(n-2)
class Solution:
def jumpFloor(self, number):
# write code here
if number == 0:
return 0
n1 = 0
n2 = 1
j = 0
while(j<number):
res = n1 + n2
n1 = n2
n2 = res
j += 1
return res
8.一隻青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的臺階總共有多少種跳法。
# -*- coding:utf-8 -*-
#找規律
class Solution:
def jumpFloorII(self, number):
# write code here
return 2**(number-1)
9.我們可以用2*1的小矩形橫着或者豎着去覆蓋更大的矩形。請問用n個2*1的小矩形無重疊地覆蓋一個2*n的大矩形,總共有多少種方法?
# -*- coding:utf-8 -*-
#仍爲斐波納鍥數列 f(n) = f(n-1) + f(n-2)
#f(n-1) 爲n步中第一步豎着覆蓋的方法
#f(n-2) 爲n步中前兩步橫着覆蓋的方法
class Solution:
def rectCover(self, number):
# write code here
if number == 0:
return 0
n1 = 0
n2 = 1
j = 0
while(j < number):
res = n1 + n2
n1 = n2
n2 = res
j += 1
return res
10.輸入一個整數數組,實現一個函數來調整該數組中數字的順序,使得所有的奇數位於數組的前半部分,所有的偶數位於數組的後半部分,並保證奇數和奇數,偶數和偶數之間的相對位置不變。
# -*- coding:utf-8 -*-
class Solution:
def reOrderArray(self, array):
# write code here
lst_ji = []
lst_ou = []
if len(array) == 0:
return []
for i in array:
if i%2 == 0:
lst_ou.append(i)
else:
lst_ji.append(i)
lst_ji.extend(lst_ou)
return lst_ji
11.輸入一個鏈表,輸出該鏈表中倒數第k個結點。
# -*- coding:utf-8 -*-
#先求出鏈表的長度l,然後讓鏈表從頭開始重新走l-k
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindKthToTail(self, head, k):
# write code here
if head == None:
return None
l = 0
curNode = head
while(curNode != None):
l += 1
curNode = curNode.next
if (l<k):
return None
curNode = head
for i in range(l-k):
curNode = curNode.next
return curNode
12.輸入一個鏈表,反轉鏈表後,輸出新鏈表的表頭。
# -*- coding:utf-8 -*-
# 先保存當前節點的下一個節點,然後把當前節點指向上一個節點,循環
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if pHead == None:
return None
preNode = None
curNode = pHead
while(curNode!=None):
nextNode = curNode.next
curNode.next = preNode
preNode = curNode
curNode = nextNode
return preNode
13.輸入兩個單調遞增的鏈表,輸出兩個鏈表合成後的鏈表,當然我們需要合成後的鏈表滿足單調不減規則。
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回合併後列表
def Merge(self, pHead1, pHead2):
# write code here
if pHead1 == None:
return pHead2
if pHead2 == None:
return pHead1
p1 = pHead1
p2 = pHead2
flag = 0
while(p1 and p2):
if (flag == 0):
if p1.val > p2.val:
root = p2
curNode = root
p2 = p2.next
flag = 1
else:
root = p1
curNode = root
flag = 1
p1 = p1.next
else:
if p1.val > p2.val:
curNode.next = p2
p2 = p2.next
else:
curNode.next = p1
p1 = p1.next
curNode = curNode.next
if p1 == None:
curNode.next = p2
else:
curNOde.next = p1
return root