当前位置:网站首页>299. 猜数字游戏
299. 猜数字游戏
2022-04-23 15:58:00 【张.Joshua】
299. 猜数字游戏
- 题号:力扣299
- 知识点:哈希表,字符串,计数
- 目标完成度:23/150
- 总结
题干:
思路:
- 1.总体思路:先找‘公牛’,然后删除公牛后再找‘奶牛’。
- 2.具体实现:
- (1)将两个字符串放入到字典中,构建散列表。
- (2)第一次遍历,查找‘公牛’,即索引位置和字符都相同的元素,查找到后将两个散列表中的value减1,这一步有点类似消消乐?
- (3)第二次遍历,查找‘奶牛’(在上一步中公牛已经被消掉了),如果元素
j
在两个散列表中都存在且value>0,则说明是母牛
class Solution:
def getHint(self, secret: str, guess: str) -> str:
hashmap = {
}
hashmap2 = {
}
ret = [0, 0]
for s in secret:
if s in hashmap:
hashmap[s] += 1
else:
hashmap[s] = 1
for g in guess:
if g in hashmap2:
hashmap2[g] += 1
else:
hashmap2[g] = 1
for i in range(len(guess)):
if guess[i] == secret[i]:
ret[0] += 1
hashmap[guess[i]] -= 1
hashmap2[guess[i]] -= 1
for j in guess:
if hashmap2[j] > 0:
if j in hashmap:
if hashmap[j] > 0:
ret[1] += 1
hashmap[j] -= 1
hashmap2[j] -= 1
ans = str(ret[0]) + 'A' + str(ret[1]) + 'B'
return ans
- 参考:题解中更简单的方法
版权声明
本文为[张.Joshua]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44742084/article/details/124361792
边栏推荐
猜你喜欢
单体架构系统重新架构
matplotlib教程05---操作图像
Modèle de Cluster MySQL et scénario d'application
Vision of building interstellar computing network
Website pressure measurement tools Apache AB, webbench, Apache jemeter
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
Multi level cache usage
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
Best practices of Apache APIs IX high availability configuration center based on tidb
Large factory technology implementation | industry solution series tutorials
随机推荐
PS add texture to picture
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
JS regular determines whether the port path of the domain name or IP is correct
Simple usage of dlopen / dlsym / dlclose
Intersection, union and difference sets of spark operators
volatile的含义以及用法
One brush 313 sword finger offer 06 Print linked list from end to end (E)
Configuration of multi spanning tree MSTP
Website pressure measurement tools Apache AB, webbench, Apache jemeter
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
建设星际计算网络的愿景
捡起MATLAB的第(7)天
Spark 算子之coalesce与repartition
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
捡起MATLAB的第(9)天
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
Spark 算子之groupBy使用
How do you think the fund is REITs? Is it safe to buy the fund through the bank
Leetcode-396 rotation function
shell_2