力扣算法题03-05
数组中重复的数字
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
repeatDict = {}
for num in nums:
if num not in repeatDict:
repeatDict[num] = 1
else:
return num
二维数组中的查找
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
class Solution:
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
for one in matrix:
for two in one:
if target == two:
return True
return False
替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
class Solution:
def replaceSpace(self, s: str) -> str:
res = []
for a in s:
if a == " ":
res.append("%20")
else:
res.append(a)
return "".join(res)