符合通讯稿要满足 :镜像号码 A + 原号码 B = 镜像号码 B + 原号码 A
即满足 : 原号码 B -镜像号码 B = 原号码 A - 镜像号码 A
那么先遍历一遍构造字典
再遍历一遍统计数量并取模
class Solution:
def numberOfPairs(self, nums: List[int]) -> int:
Mod= 10e8+7
res = defaultdict(int)
for num in nums:
temp=int(str(num)[::-1])
res[num-temp]+=1
cnt = 0
for k,v in res.items():
v= ((v-1)*v/2)%Mod
cnt=(cnt+v)%Mod
return int(cnt)
简单的遍历题,
每当遍历到新的‘W’时开始对周边进行搜索,对所有遇到的’W‘进行涂色
本来想用bfs的,结果不知道哪里写错了过不了,后来改用dfs解决
class Solution:
def lakeCount(self, field: List[str]) -> int:
row = len(field)
col = len(field[0])
road = [list(f) for f in field]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
cnt = 0
def dfs(x, y):
road[x][y] = '.'
for a, b in zip(dx, dy):
tx, ty = a + x, b + y
if 0 <= tx < row and 0 <= ty < col and road[tx][ty] == 'W':
dfs(tx, ty)
return
for i in range(row):
for j in range(col):
if road[i][j] == 'W':
cnt += 1
dfs(i, j)
return cnt
首先对每个数组中的数,求出2,3因子个数
剩下无2,3因子的数不同则无法构造
并使用tar数组加速因子计算速度
class Solution:
def minOperations(self, numbers: List[int]) -> int:
def func2(data, pos):
if pos == 2:
tar = [2, 4, 8, 16, 32, 64]
else:
tar = [3, 9, 27, 81, 243, 729]
res, cnt = 1, 6
while cnt != 0:
if data % tar[cnt - 1] == 0:
data //= tar[cnt - 1]
res += cnt
else:
cnt -= 1
return res, data
t2, da = func2(numbers[0], 2)
t3, da = func2(da, 3)
check = [(t2, t3)]
m2, m3 = t2, t3
vis = da
for i in range(1, len(numbers)):
t2, da = func2(numbers[i], 2)
t3, da = func2(da, 3)
if da != vis:
return -1
m2 = max(m2, t2)
m3 = max(m3, t3)
check.append((t2, t3))
s = 0
for c in check:
s += (m2 - c[0] + m3 - c[1])
return s
期望题不太想做啊,等以后有空再看吧
orz orz