867.转置矩阵
class Solution:
def transpose(self, A):
A[::] = zip(*A)
return A
def transpose1(self, A):
if len(A) == 0: return []
r, c = len(A), len(A[0])
return [[A[i][j] for i in range(r)] for j in range(c)]
if __name__ == '__main__':
solu = Solution()
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(solu.transpose(A))
三个数的最大乘积
nums = sorted(nums)
return max(nums[-1]*nums[-2]*nums[-3],nums[-1]*nums[0]*nums[1])
数组中的众数
c=collections.Counter(nums)
return c.most_common(1)[0][0]
切片的知识补充
A=[i for i in range(100)]
A[:10] #前10
A[-10:] #后10
A[10:20] #前11-20
A[:10:2] #前10,每2个取一个值
A[::5] #所有数,每5个取一个值
B=A[:] #原样复制一个list
单调数列
class Solution:
def isMonotonic(self, A):
n=len(A)
count1=0
count2=0
for i in range(1,n):
tmp = A[i-1] - A[i]
if tmp >=0:
count1+=1
if tmp <=0:
count2+=1
if count1==n-1 or count2==n-1:
return True
else:
return False
方法二
B=sorted(A)
C=sorted(A,reverse=True)
if A==B or A==C:
return True
else:
return False