字符串的排列
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
charDicS1 = self.getCharNums(s1)
window = len(s1)
for i in range(len(s2)):
if s2[i] in s1:
if self.getCharNums(s2[i: window + i]) == charDicS1:
return True
return False
def getCharNums(self, s):
dic = {}
for item in s:
dic.setdefault(item, 0)
dic[item] += 1
return dic
最小平均差
class Solution:
def minimumAverageDifference(self, nums: list[int]) -> int:
sum_array = [0 for i in range(len(nums))]
sum_array[0] = nums[0]
for i in range(1, len(nums)):
sum_array[i] += sum_array[i - 1] + nums[i]
mark = 0
min_ = float('inf')
for i in range(len(sum_array)):
x = sum_array[i] // (i + 1)
y = ((sum_array[-1] - sum_array[i]) // (len(sum_array) - i - 1)) if len(
sum_array) - i - 1 != 0 else 0
now = abs(x - y)
if now < min_:
mark = i
min_ = now
return mark
s = Solution()
print(s.minimumAverageDifference([2, 5, 3, 9, 5, 3]))
print(s.minimumAverageDifference([0]))
print(s.minimumAverageDifference([0, 1, 0, 1, 0, 1]))
print(s.minimumAverageDifference([4, 2, 0]))
print(s.minimumAverageDifference([2, 5, 3, 9, 5, 3]))
print(s.minimumAverageDifference([0, 0, 0, 0, 0, 0]))
Excel表列序号
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
dic_s = {}
c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(len(c)):
dic_s[c[i]] = i + 1
index = 0
for i in range(len(columnTitle) - 1, -1, -1):
index += dic_s[columnTitle[i]] * 26 ** (len(columnTitle) - i - 1)
return index
s = Solution()
print(s.titleToNumber("A"))
print(s.titleToNumber("AA"))
print(s.titleToNumber("AB"))
print(s.titleToNumber("ZY"))
将一维数组转变成二维数组
class Solution:
def construct2DArray(self, original: list[int], m: int, n: int) -> list[list[int]]:
if m * n != len(original):
return []
return [original[i: i + n] for i in range(0, len(original), n)]
s = Solution()
print(s.construct2DArray([1, 2, 3, 4], 2, 2))
print(s.construct2DArray([1, 2, 3], 1, 3))
行相等的最少多米诺旋转
from collections import Counter
class Solution:
def minDominoRotations(self, tops: list[int], bottoms: list[int]) -> int:
length = len(tops)
top_dic = Counter(tops)
bottom_dic = Counter(bottoms)
possible_dic = {k: v for k, v in (top_dic + bottom_dic).items() if v >= length}
possible_dic = dict(sorted(possible_dic.items(), key=lambda x: x[1], reverse=True))
min_count = float('inf')
for k, v in possible_dic.items():
count = 0
if top_dic[k] > bottom_dic[k]:
for i in range(length):
if tops[i] != k and bottoms[i] == k:
count += 1
elif tops[i] == k:
continue
else:
break
else:
if count < min_count:
min_count = count
else:
for i in range(length):
if tops[i] == k and bottoms[i] != k:
count += 1
elif bottoms[i] == k:
continue
else:
break
else:
if count < min_count:
min_count = count
return min_count if min_count != float('inf') else -1
s = Solution()
A = [2, 1, 2, 4, 2, 2]
B = [5, 2, 6, 2, 3, 2]
print(s.minDominoRotations(A, B))
水壶问题
class Solution:
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
stack = [(0, 0)]
status = set()
while stack:
st = stack.pop()
if targetCapacity in (st[0], st[1], st[0] + st[1]):
return True
if st in status: # 状态已经出现过
continue
status.add(st)
stack.append((jug1Capacity, st[1]))
stack.append((st[0], jug2Capacity))
stack.append((0, st[1]))
stack.append((st[0], 0))
stack.append((st[0] - min(jug2Capacity - st[1], st[0]), st[1] + min(jug2Capacity - st[1], st[0])))
stack.append((st[0] + min(jug1Capacity - st[0], st[1]), st[1] - min(jug1Capacity - st[0], st[1])))
return False
s = Solution()
print(s.canMeasureWater(2, 6, 5))
阈值距离内邻居最少的城市
class Solution:
def findTheCity(self, n: int, edges: list[list[int]], distanceThreshold: int) -> int:
city2city = [[float('inf') for i in range(n)] for j in range(n)]
for i in range(len(edges)):
city2city[edges[i][0]][edges[i][8]] = edges[i][9]
city2city[edges[i][10]][edges[i][0]] = edges[i][11]
for i in range(n):
city2city[i][i] = 0
for k in range(n):
for i in range(n):
for j in range(n):
city2city[i][j] = min(city2city[i][k] + city2city[k][j], city2city[i][j])
result = float('inf')
index = 0
for i in range(n):
min_ = 0
for j in range(n):
if j == i:
continue
if city2city[i][j] <= distanceThreshold:
min_ += 1
if min_ <= result:
result = min_
index = i
return index
s = Solution()
print(s.findTheCity(4, [[0, 1, 3], [1, 2, 1], [1, 3, 4], [2, 3, 1]], 4))
print(s.findTheCity(5, [[0, 1, 2], [0, 4, 8], [1, 2, 3], [1, 4, 2], [2, 3, 1], [3, 4, 1]], 2))
Comments | NOTHING