题19:顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self,matrix):
result = []
while matrix:
result += matrix.pop(0)
if not matrix or not matrix[0]:
break
matrix = self.turn(matrix)
return result
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self,matrix):
result = []
while matrix:
result += matrix.pop(0)
if not matrix or not matrix[0]:
break
matrix = self.turn(matrix)
return result
def turn(self,matrix):
num_r = len(matrix)
num_c = len(matrix[0])
newmat = []
for i in range(num_c):
newmat2 = []
for j in range(num_r):
newmat2.append(matrix[j][i])
newmat.append(newmat2)
newmat.reverse()
return newmat
num_r = len(matrix)
num_c = len(matrix[0])
newmat = []
for i in range(num_c):
newmat2 = []
for j in range(num_r):
newmat2.append(matrix[j][i])
newmat.append(newmat2)
newmat.reverse()
return newmat
题20:包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.arr=[]
def push(self, node):
self.arr.append(node)
def pop(self):
return self.arr.pop()
def top(self):
return self.arr[-1]
def min(self):
return min(self.arr)
class Solution:
def __init__(self):
self.arr=[]
def push(self, node):
self.arr.append(node)
def pop(self):
return self.arr.pop()
def top(self):
return self.arr[-1]
def min(self):
return min(self.arr)
题21:栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
# -*- coding:utf-8 -*-
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
stack = [] # 存入pushV中取出的数据
while popV:
# 如果第一个元素相等,直接弹出
if pushV and popV[0] == pushV[0]:
popV.pop(0)
pushV.pop(0)
# 如果stack的最后一个元素与popV中第一个元素相等,将两个元素都弹出
elif stack and stack[-1] == popV[0]:
stack.pop()
popV.pop(0)
# 如果pushV中有数据,压入stack
elif pushV:
stack.append(pushV.pop(0))
else:
return False
return True
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
stack = [] # 存入pushV中取出的数据
while popV:
# 如果第一个元素相等,直接弹出
if pushV and popV[0] == pushV[0]:
popV.pop(0)
pushV.pop(0)
# 如果stack的最后一个元素与popV中第一个元素相等,将两个元素都弹出
elif stack and stack[-1] == popV[0]:
stack.pop()
popV.pop(0)
# 如果pushV中有数据,压入stack
elif pushV:
stack.append(pushV.pop(0))
else:
return False
return True
题22:从上往下打印二叉树
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
def PrintFromTopToBottom(self, root):
# write code here
if not root:
return []
currentStack = [root]
res = []
while currentStack:
nextStack = []
for i in currentStack:
if i.left:
nextStack.append(i.left)
if i.right:
nextStack.append(i.right)
res.append(i.val)
currentStack = nextStack
return res
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
def PrintFromTopToBottom(self, root):
# write code here
if not root:
return []
currentStack = [root]
res = []
while currentStack:
nextStack = []
for i in currentStack:
if i.left:
nextStack.append(i.left)
if i.right:
nextStack.append(i.right)
res.append(i.val)
currentStack = nextStack
return res
题23:字符串的排列
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
# -*- coding:utf-8 -*-
class Solution:
def Permutation(self,ss):
res = list()
def Traversal(ss,join_ss=''):
if ss:
for i,s in enumerate(ss):
sub_ss = ss[:i] + ss[i+1:]
Traversal(sub_ss, join_ss+s)
elif join_ss and join_ss not in res:
res.append(join_ss)
if ss:
Traversal(ss)
return res
题24: 数组中出现次数超过一半的数字
class Solution:
def Permutation(self,ss):
res = list()
def Traversal(ss,join_ss=''):
if ss:
for i,s in enumerate(ss):
sub_ss = ss[:i] + ss[i+1:]
Traversal(sub_ss, join_ss+s)
elif join_ss and join_ss not in res:
res.append(join_ss)
if ss:
Traversal(ss)
return res
题24: 数组中出现次数超过一半的数字
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
# -*- coding:utf-8 -*-
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
number=[]
for i in numbers:
if numbers.count(i)>len(numbers)//2:
number.append(i)
if len(number)>0:
for j in list(set(number)):
return j
else:
return 0
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
number=[]
for i in numbers:
if numbers.count(i)>len(numbers)//2:
number.append(i)
if len(number)>0:
for j in list(set(number)):
return j
else:
return 0