题目一:
题目链接: https://leetcode-cn.com/problems/find-lucky-integer-in-an-array/
解题思路:
利用hash表保存每个数字的出现次数
然后遍历hash表,判断出现次数和数字是否相同,保存并返回最大的一个数字
代码实现:
class Solution:
def findLucky(self, arr: List[int]) -> int:
rec_map = {}
for a in arr:
if a in rec_map:
rec_map[a] += 1
else:
rec_map[a] = 1
res = []
for a, b in rec_map.items():
if a == b:
res.append(a)
if 0 == len(res):
return -1
else:
return max(res)
题目二:
题目链接: https://leetcode-cn.com/problems/count-number-of-teams/
解题思路:
回溯 + 剪枝
分成两部分回溯:逐步增大的和逐步减小的
如果当前已满足3个数字,则退出进行剪枝
或者直接写3层循环遍历
代码实现:
回溯 + 剪枝
class Solution:
def numTeams(self, rating: List[int]) -> int:
def create_res(start, rating, res, path, search_type):
if len(path) == 3:
res[0] += 1
return
for index in range(start, len(rating)):
if 0 == len(path):
path.append(rating[index])
create_res(index + 1, rating, res, path, 0)
create_res(index + 1, rating, res, path, 1)
path.pop()
elif search_type == 0 and rating[index] > path[-1]:
path.append(rating[index])
create_res(index + 1, rating, res, path, search_type)
path.pop()
elif search_type == 1 and rating[index] < path[-1]:
path.append(rating[index])
create_res(index + 1, rating, res, path, search_type)
path.pop()
return
res = [0]
create_res(0, rating, res, [], 0)
return res[0]
三层遍历:
class Solution:
def numTeams(self, rating: List[int]) -> int:
res = 0
for i in range(0, len(rating) - 2):
for j in range(i + 1, len(rating) - 1):
for k in range(j + 1, len(rating)):
if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:
res += 1
return res
题目三:
题目链接: https://leetcode-cn.com/problems/design-underground-system/
解题思路:
添加两个hash表保存信息
- id为key,保存用户的入站时间和站名(__user_rec)
- 出入站为key,保存出入站的总间隔时间和人数(__station_rec)
每个接口的实现逻辑如下:
- checkIn(int id, string stationName, int t)
- 如果id在__user_rec中,说明用户之前已经checkin,当前checkin信息无效,不保存
- 如果不存在,则记录__user_rec[id] = [stationName, t],保存checkin的车站名和时间点
- checkOut(int id, string stationName, int t)
- 如果id不在__user_rec,说明用户之前没有过checkin操作,当前checkout操作无效
- 如果存在,则进行下记操作:
- 获取并删除id用户在__user_rec中的记录
- 计算出入站所需时间
- 根据出入站的车站名,生成key值,更新__station_rec中信息
- getAverageTime(string startStation, string endStation)
- 根据起终点站名,生成key值,并判断key在__station_rec中是否存在
- 如果存在,返回对应的平均时间
- 如果不存在,返回0
代码实现:
class UndergroundSystem:
__user_rec = {}
__station_rec = {}
def __init__(self):
self.__user_rec = {}
self.__station_rec = {}
def checkIn(self, id: int, stationName: str, t: int) -> None:
if id not in self.__user_rec:
self.__user_rec[id] = [stationName, t]
def checkOut(self, id: int, stationName: str, t: int) -> None:
in_info = self.__user_rec.pop(id, None)
if not in_info:
return
in_station = in_info[0]
in_time = in_info[1]
out_station = stationName
out_time = t
station_str = in_station + '-' + out_station
diff_time = t - in_time
if station_str not in self.__station_rec:
self.__station_rec[station_str] = [diff_time, 1]
else:
self.__station_rec[station_str][0] += diff_time
self.__station_rec[station_str][1] += 1
def getAverageTime(self, startStation: str, endStation: str) -> float:
station_str = startStation + '-' + endStation
if station_str not in self.__station_rec:
return 0.0
return self.__station_rec[station_str][0] / self.__station_rec[station_str][1]
# Your UndergroundSystem object will be instantiated and called as such:
# obj = UndergroundSystem()
# obj.checkIn(id,stationName,t)
# obj.checkOut(id,stationName,t)
# param_3 = obj.getAverageTime(startStation,endStation)
题目四:
题目链接: https://leetcode-cn.com/problems/find-all-good-strings/
解题思路:
自己没做出来...
等看完排名前三大佬的代码,再来更新