题目描述1:二维数组中的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
Python测试:
// An highlighted block
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
rows = len(array) - 1
cols= len(array[0]) - 1
i = rows
j = 0
while j<=cols and i>=0:
if target<array[i][j]:
i -= 1
elif target>array[i][j]:
j += 1
else:
return True
return False
if __name__ == "__main__":
a = Solution()
target = 3
array =[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
print(a.Find(target, array))
题目描述2:数组中重复的数字
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
Python测试:
// An highlighted block
class Solution:
def duplicate(self,alist):
tmp = []
for item in alist:
if alist.count(item) > 1:
tmp.append(item)
tmp = list(set(tmp))
return tmp
if __name__ == '__main__':
a_list = [1,3,5,3,1,2,1,6]
obj = Solution()
dup = obj.duplicate(a_list)
print(dup)
题目描述3:构建乘积数组
给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。
Python测试:
// An highlighted block
class Solution:
def multiply(self, A):
B = [1]*len(A)
for i in range(0,len(A)):
for j in range(0,len(B)):
if i!=j:
B[i] *= A[j]
return B
if __name__ == '__main__':
a_list = [1,3,5]
obj = Solution()
dup = obj.multiply(a_list)
print(dup)
相关知识点回顾:
总结:
二维数组中的查找:
https://blog.csdn.net/qq_38441207/article/details/88355763
数组中重复的数字:
https://blog.csdn.net/qq_38441207/article/details/88781388
构建乘积数组:
https://blog.csdn.net/qq_38441207/article/details/88781471