尽量不用python特有的能够一步到位的函数,答案均已在牛客网中提交通过。
1.数组:二维数组中的查找
1 '''
2 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数3 '''
4 classSolution:5 #array 二维列表
6 defFind(self, target, array):7 row =len(array)8 col =len(array[0])9
10 #从右上角开始,比target大则往左寻找,比target小则往下寻找
11 index_row =012 index_col = col - 1
13 while index_row < row and index_col >=0:14 if array[index_row][index_col] ==target:15 returnTrue16 elif array[index_row][index_col] >target:17 index_col -= 1
18 else:19 index_row += 1
20 return False
View Code
2.字符串:替换空格
1 '''
2 请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy3 '''
4 classSolution:5 #s 源字符串
6 defreplaceSpace(self, s):7 space_num = s.count(' ')8 lenth =len(s)9 L =[]10 i =011
12 while i
<
<<