题目(20240909~20240913)
面试题 16.15. 珠玑妙算
珠玑妙算游戏(the game of master mind)的玩法如下:
计算机有4个槽,每个槽放一个球,颜色可能是红色(R)、黄色(Y)、绿色(G)或蓝色(B)。
例如,计算机可能有RGGB 4种(槽1为红色,槽2、3为绿色,槽4为蓝色)。作为用户,你试图猜出颜色组合。
打个比方,你可能会猜YRGB。要是猜对某个槽的颜色,则算一次“猜中”;要是只猜对颜色但槽位猜错了,则算一次“伪猜中”。注意,“猜中”不能算入“伪猜中”。
给定一种颜色组合solution
和一个猜测guess
,编写一个方法,返回猜中和伪猜中的次数answer
,其中answer[0]
为猜中的次数,answer[1]
为伪猜中的次数。
示例:
输入: solution=“RGBY”,guess=“GGRR”
输出: [1,1]
解释: 猜中1次,伪猜中1次。
提示:
len(solution) = len(guess) = 4
solution
和guess
仅包含"R","G","B","Y"
这4种字符
方法1:计数器
from typing import List
from collections import Counter
class Solution:
def masterMind(self, solution: str, guess: str) -> List[int]:
total = sum((Counter(solution) & Counter(guess)).values())
gr = sum([1 for i in range(4) if solution[i] == guess[i]])
return [gr, total - gr]
if __name__ == '__main__':
s = Solution()
# 面试题 16.15.珠玑妙算 - 示例1
solution = "RGBY"
guess = "GGRR"
res = s.masterMind(solution, guess)
print(res) # 输出:[1, 1]
方法2:遍历
from typing import List
class Solution:
def masterMind(self, solution: str, guess: str) -> List[int]:
res = [0, 0]
for i in range(4):
# 猜中
if solution[i] == guess[i]:
res[0] += 1
for i in range(4):
# 伪猜中
if guess[i] in solution:
solution = solution.replace(guess[i], "", 1)
res[1] += 1
# 伪猜中 - 猜中
res[1] -= res[0]
return res
if __name__ == '__main__':
s = Solution()
# 面试题 16.15.珠玑妙算 - 示例1
solution = "RGBY"
guess = "GGRR"
res = s.masterMind(solution, guess)
print(res) # 输出:[1, 1]
方法3:哈希表
from typing import List
class Solution:
def masterMind(self, solution: str, guess: str) -> List[int]:
res, hs, hg = [0, 0], {}, {}
# 统计solution和guess中每个字符出现的次数
for i in range(len(solution)):
hs[solution[i]] = hs.get(solution[i], 0) + 1
hg[guess[i]] = hg.get(guess[i], 0) + 1
# 猜中
for i in range(len(guess)):
if guess[i] == solution[i]:
res[0] += 1
hg[guess[i]] -= 1
hs[guess[i]] -= 1
# 伪猜中
for key in hg.keys():
if hs.get(key) is not None and hg[key] > 0:
res[1] += min(hg[key], hs[key])
return res
if __name__ == '__main__':
s = Solution()
# 面试题 16.15.珠玑妙算 - 示例1
solution = "RGBY"
guess = "GGRR"
res = s.masterMind(solution, guess)
print(res) # 输出:[1, 1]
面试题 08.06. 汉诺塔问题
在经典汉诺塔问题中,有 3 根柱子及 N 个不同大小的穿孔圆盘,盘子可以滑入任意一根柱子。一开始,所有盘子自上而下按升序依次套在第一根柱子上(即每一个盘子只能放在更大的盘子上面)。
移动圆盘时受到以下限制:
(1) 每次只能移动一个盘子;
(2) 盘子只能从柱子顶端滑出移到下一根柱子;
(3) 盘子只能叠在比它大的盘子上。
请编写程序,用栈将所有盘子从第一根柱子移到最后一根柱子。
你需要原地修改栈。
示例 1:
输入:A = [2, 1, 0], B = [], C = []
输出:C = [2, 1, 0]
示例 2:
输入:A = [1, 0], B = [], C = []
输出:C = [1, 0]
提示:
- A中盘子的数目不大于14个。
代码
from typing import List
class Solution:
def hanota(self, A: List[int], B: List[int], C: List[int]) -> None:
"""
Do not return anything, modify C in-place instead.
"""
n = len(A)
self.move(n, A, B, C)
# 定义move函数移动汉诺塔
def move(self, n, A, B, C):
if n == 1:
C.append(A[-1])
A.pop()
else:
self.move(n - 1, A, C, B) # 将A上面n-1个通过C移到B
C.append(A[-1]) # 将A最后一个移到C
A.pop() # 这时,A空了
self.move(n - 1, B, A, C) # 将B上面n-1个通过空的A移到C
return C
if __name__ == '__main__':
s = Solution()
# 面试题 08.06. 汉诺塔问题 - 示例1
A = [2, 1, 0]
B = []
C = []
s.hanota(A, B, C)
print(C) # [2, 1, 0]
# 示例2
A = [1, 0]
B = []
C = []
s.hanota(A, B, C)
print(C) # [1, 0]