Skip to content

Commit

Permalink
2019-07-21
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Jul 21, 2019
1 parent 0d4a198 commit 4c81607
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 0172.阶乘后的零/0172-阶乘后的零.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
while n > 4:
n //= 5
res += n
return res

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution(object):
def numEquivDominoPairs(self, dominoes):
"""
:type dominoes: List[List[int]]
:rtype: int
"""
from collections import defaultdict
dic = defaultdict(int)
res = 0
for i, x in enumerate(dominoes):
pair = (x[1], x[0])
if x[0] < x[1]:
pair = (x[0], x[1])
res += dic[pair]
dic[pair] += 1

return res
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution(object):
def shortestAlternatingPaths(self, n, red_edges, blue_edges):
"""
:type n: int
:type red_edges: List[List[int]]
:type blue_edges: List[List[int]]
:rtype: List[int]
"""
from collections import defaultdict, deque
red_nei, blue_nei = defaultdict(set), defaultdict(set)

for start, end in red_edges: #记录下所有的从红色路径可以到达的邻居节点
red_nei[start].add(end)
for start, end in blue_edges: #记录下所有的从蓝色路径可以到达的邻居节点
blue_nei[start].add(end)

visited = set()
queue = deque()
queue.append((0, -1)) # -1 无色, 0 红色, 1蓝色
distance = 0
res = [-1] * n
while queue:
next_queue = deque()
for cur, color in queue:
if res[cur] == -1: #BFS保障了第一次到达终点时的距离一定是最短距离
res[cur] = distance
if color == -1 or color == 0: #当前是红色,下一次在蓝色里找
for nei in blue_nei[cur]:
if (cur, nei, 1) not in visited:
visited.add((cur, nei, 1))
next_queue.append((nei, 1))
if color == -1 or color == 1: #当前是蓝色,下一次在红色里找
for nei in red_nei[cur]:
if (cur, nei, 0) not in visited:
visited.add((cur, nei, 0))
next_queue.append((nei, 0))
queue = next_queue
distance += 1
return res

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution(object):
def maxAbsValExpr(self, arr1, arr2):
"""
:type arr1: List[int]
:type arr2: List[int]
:rtype: int
"""
record = [-2 ** 31] * 8

for i in range(len(arr1)):
for k in range(8): #生成八种可能的结果
t = 0
if k & 1:
t += arr1[i]
else:
t -= arr1[i]
if k & 2:
t += arr2[i]
else:
t -= arr2[i]
if k & 4:
t += i
else:
t -= i
# print k, t
record[k] = max(record[k], t)
# print record
res = 0
for k in range(8):
# print k, ~k
res = max(res, record[k] + record[-k-1]) #第一项和最后一项,第二项和倒数第二项,两项之和一定是满足表达式的
return res


0 comments on commit 4c81607

Please sign in to comment.