记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
6/28 815. Bus Routes 公交路线
m记录每个站拥有的公交线路
双向BFS 考虑站点可以坐的公交线路
mem记录已坐过的线路 乘坐同一条线路无意义
从起点、终点分别获取可以乘坐的公交线路 l1,l2
每次选取较少的情况考虑 len(l1)>len(l2)
如果坐到了同一条公交线必定可以到达公交线路所在站点 返回答案
统计线路中所有站点下一次能够乘坐的未使用公交线路 m[station]-mem
def numBusesToDestination(routes, source, target):
"""
:type routes: List[List[int]]
:type source: int
:type target: int
:rtype: int
"""
if source == target:
return 0
from collections import defaultdict
m = defaultdict(set)
for i,stations in enumerate(routes):
for station in stations:
m[station].add(i)
routes = [set(x) for x in routes]
l1 = m[source]
l2 = m[target]
step = 0
mem = set()
while l1 and l2:
if len(l1)>len(l2):
l1,l2 = l2,l1
step +=1
tmp = set()
for route in l1:
if route in l2:
return step
if route in mem:
continue
mem.add(route)
for station in routes[route]:
tmp.update(m[station]-mem)
l1 = tmp
return -1
6/29 168. Excel Sheet Column Title Excel表列名称
26进制 没有0 每次需要减1
def convertToTitle(columnNumber):
"""
:type columnNumber: int
:rtype: str
"""
ans = ""
while columnNumber:
columnNumber-=1
v = columnNumber%26
c = chr(ord('A')+v)
ans =c+ans
columnNumber = columnNumber//26
return ans
6/30 剑指 Offer 37. 序列化二叉树
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
l=[root]
ret=[]
while len(l)>0:
tmp = l.pop(0)
if tmp==None:
ret.append("null")
continue
ret.append(tmp.val)
left,right = tmp.left,tmp.right
l.append(left)
l.append(right)
for i in range(len(ret)-1,-1,-1):
if ret[i]!="null":
return ret[:i+1]
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
if not data:
return
root = TreeNode(data[0])
l = [root]
i=1
while len(l)>0:
tmp = l.pop(0)
if tmp==None or i>=len(data):
continue
tmp.left = None if data[i]=="null" else TreeNode(data[i])
if i+1<len(data):
tmp.right = None if data[i+1]=="null" else TreeNode(data[i+1])
else:
tmp.right = None
i+=2
l.append(tmp.left)
l.append(tmp.right)
return root
7/1 LCP 07. 传递信息
BFS
def numWays(n, relation, k):
"""
:type n: int
:type relation: List[List[int]]
:type k: int
:rtype: int
"""
from collections import defaultdict
nextway = defaultdict(list)
for way in relation:
fro,to = way[0],way[1]
nextway[fro].append(to)
m = defaultdict(int)
m[0] = 1
while k>0:
k-=1
tmp = defaultdict(int)
for fro in m:
v = m[fro]
for to in nextway[fro]:
tmp[to] += v
m = tmp
return m[n-1]
7/2 1833. Maximum Ice Cream Bars 雪糕的最大数量
从小到大排序
先拿小的肯定拿的最多
def maxIceCream(costs, coins):
"""
:type costs: List[int]
:type coins: int
:rtype: int
"""
costs.sort()
num = 0
cost = 0
for i in costs:
cost += i
if cost>coins:
return num
else:
num +=1
return num
7/3 451. Sort Characters By Frequency 根据字符出现频率排序
统计每个字符出现次数 按次数排序
def frequencySort(s):
"""
:type s: str
:rtype: str
"""
from collections import defaultdict
m = defaultdict(int)
for c in s:
m[c]+=1
l=[]
for c in m:
l.append((c,m[c]))
l.sort(key = lambda x: x[1],reverse=True)
ans = ""
for c,num in l:
ans += c*num
return ans
7/4 645. Set Mismatch 错误的集合
set去重找到重复数值 1-n求总和减去当前未重复值 得到缺失数值
def findErrorNums(nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
n = len(nums)
pres = sum(nums)
s = sum(set(nums))
v1 = pres-s
v2 = int((1+n)*n/2-s)
return [v1,v2]