# 19、顺时针打印矩阵

# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表，需要返回列表
def printMatrix(self, matrix):
# write code here
rows=len(matrix)
cols=len(matrix[0])
result=[]
if rows==0 and cols==0:
return result
left,right,top,bottom = 0,cols-1,0,rows-1

while left<=right and top<=bottom:
for i in range(left,right+1):
result.append(matrix[top][i])
for i in range(top+1,bottom+1):
result.append(matrix[i][right])
if top!=bottom:
for i in range(right-1,left-1,-1):
result.append(matrix[bottom][i])
if left!=right:
for i in range(bottom-1,top,-1):
result.append(matrix[i][left])
left+=1
right-=1
top+=1
bottom-=1
return result


# 28、数组中出现次数超过一半的数字

# -*- coding:utf-8 -*-
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
numbers.sort()
theone = numbers[len(numbers)/2]
if numbers.count(theone)>len(numbers)/2:
return theone
return 0


# 29、最小的K个数

# -*- coding:utf-8 -*-
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if tinput == [] or k > len(tinput):
return []
tinput.sort()
return tinput[: k]


# 30、连续子数组的最大和

dp[i] / dp[i][j] 代表什么意思？

# -*- coding:utf-8 -*-
class Solution:
def FindGreatestSumOfSubArray(self, array):
# write code here
dp = [0 for i in range(len(array))]

for i in range(len(array)):
dp[i] = max(dp[i-1], 0) + array[i]

return max(dp)

08-29 1422
09-10 3656

08-16 4万+
08-06 1万+
04-25 1万+
09-03 3123