4/75
03.找出数组中重复的数字
try1: 遍历 超时
class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
for i in range(len(nums)-1):
for j in range(i+1,len(nums)): # 第一次写忘记+1了
if nums[i] == nums[j]:
return nums[I]
try2: 字典
class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
dic = {}
for i in range(len(nums)):
if nums[i] not in dic:
dic[nums[i]] = 1
else: # 下面多余
dic[nums[i]] += 1
for i in dic.keys():
if dic[i] > 1:
return i # 第一次写错为了return dic[I]
try3:改进字典
class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
dic = {}
for i in range(len(nums)):
if nums[i] not in dic:
dic[nums[i]] = 1
else:
return nums[I]
04.在二维数组中查找其是否包含指定元素
题解:将矩阵旋转45度,会发现每个元素的左边元素小于它,而右边元素大于它
05. 替换空格
try1: 利用字符串相关的函数
题解:不用自带函数的解法 遍历
06.打印链表
try1: 利用数组的索引进行插入
try2: 递归 利用python特性,把当前head.val加在前部,直到head不存在,即 head=None为结束标志