一,题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
class Solution:
def find(self,number,matrix):
rows=len(matrix)#行数
cols=len(matrix[0])#列数
if rows<0 and cols<0:
return False
col=0
row=rows-1
while row>=0 and col<cols:
if matrix[row][col]<number:
col+=1
elif matrix[row][col]>number:
row-=1
else:
return True#找到
return False#没找到
if __name__ == '__main__':
matrix = [[1, 3, 5, 6],
[2, 5, 8, 12],
[4, 9, 10, 17],
[6, 11, 11, 18]]
sol=Solution()
print(sol.find(17,matrix))
二,题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
"""
请实现一个函数,将一个字符串中的空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
"""
class Solution:
def replace_space(self,s):
return s.replace(' ','20%')
sol=Solution()
print(sol.replace_space('we are happy'))