# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
if number == 1:
return 1
elif number == 2:
return 2
else:
return self.jumpFloorII(number-1)*2
这题目有点鬼事,我还写了一个递归的。。。更鬼事,不如用2**(number-1)更好
10:
# -*- coding:utf-8 -*-
class Solution:
def rectCover(self, number):
# write code here
if number == 0:
return 0
if number == 1:
return 1
if number == 2:
return 2
lst = [1,2]
i = 2
while i <= number-1:
lst.append(lst[i-1] + lst[i-2])
i += 1
return lst.pop()
11:
# -*- coding:utf-8 -*-
class Solution:
def NumberOf1(self, n):
# write code here
if n < 0:
n = n & 0xffffffff
num = 0
while n > 0:
num += 1
n = n & (n-1)
return num
也可以这样:
# -*- coding:utf-8 -*-
class Solution:
def NumberOf1(self, n):
# write code here
if n < 0:
return bin(n&0xffffffff).count("1")
return bin(n).count("1")
12:
class Solution:
def Power(self, base, exponent):
# write code here
return base**exponent
13:
# -*- coding:utf-8 -*-
class Solution:
def reOrderArray(self, array):
# write code here
i = 0
j = len(array)-1
odd = []
even = []
while i <= j:
if array[i] % 2 != 0:
odd.append(array[i])
i += 1
elif array[i] % 2 == 0:
even.append(array[i])
i += 1
odd.extend(even)
return odd
14:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindKthToTail(self, head, k):
# write code here
stack = []
if head is None:
return
while head:
stack.append(head)
head = head.next
if k <= 0 or len(stack) < k:
return
return stack[-k]
15:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
if not pHead or pHead.next == None:
return pHead
pre = None
while pHead:
tmp = pHead.next
pHead.next = pre
pre = pHead
pHead = tmp
return pre
16:
# -*- 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
head = ListNode(0)
head.next = None
root = head
while pHead1 and pHead2:
if pHead1.val < pHead2.val:
head.next = pHead1
head = pHead1
pHead1 = pHead1.next
else:
head.next = pHead2
head = pHead2
pHead2 = pHead2.next
if pHead1 is None:
head.next = pHead2
if pHead2 is None:
head.next = pHead1
return root.next
17:
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def HasSubtree(self, pRoot1, pRoot2):
def preconvert(p):
if p:
return str(p.val) + preconvert(p.left) + preconvert(p.right)
else:
return ""
def midconvert(p):
if p:
return midconvert(p.left) + str(p.val) + preconvert(p.right)
else:
return ""
if pRoot2:
if preconvert(pRoot2) in preconvert(pRoot1) and midconvert(pRoot2) in midconvert(pRoot2):
return True
return False
18:
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回镜像树的根节点
def Mirror(self, root):
# write code here
if root == None:
return root
root.left,root.right = root.right,root.left
self.Mirror(root.left)
self.Mirror(root.right)
非递归:
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回镜像树的根节点
def Mirror(self, root):
# write code here
if root is None:
return root
stack = []
stack.append(root)
while stack:
x = stack.pop()
x.left,x.right = x.right,x.left
if x.left:
stack.append(x.left)
if x.right:
stack.append(x.right)
return root
19:
# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self, matrix):
# write code here
result = []
while matrix is not None:
result += matrix.pop(0)
matrix = self.xuanzhuan(matrix)
return result
def xuanzhuan(self,matrix):
if len(matrix) == 0:
return None
raw = len(matrix)
col = len(matrix[0])
result = []
for i in range(col):
result_o = []
for j in range(raw):
result_o.append(matrix[j][i])
result.append(result_o)
result.reverse()
return result