当前位置:网站首页>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
边栏推荐
- Vision of building interstellar computing network
- Named in pytoch_ parameters、named_ children、named_ Modules function
- Upgrade MySQL 5.1 to 5.611
- 捡起MATLAB的第(8)天
- 捡起MATLAB的第(2)天
- Spark 算子之partitionBy
- Why is IP direct connection prohibited in large-scale Internet
- Fastjon2他来了,性能显著提升,还能再战十年
- IronPDF for . NET 2022.4.5455
- Large factory technology implementation | industry solution series tutorials
猜你喜欢
Partitionby of spark operator
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Codejock Suite Pro v20. three
Ice -- source code analysis
捡起MATLAB的第(7)天
Configuration of multi spanning tree MSTP
王启亨谈Web3.0与价值互联网“通证交换”
Filter usage of spark operator
糖尿病眼底病变综述概要记录
Vision of building interstellar computing network
随机推荐
Nanny Anaconda installation tutorial
新动态:SmartMesh和MeshBox的合作新动向
下载并安装MongoDB
C语言自编字符串处理函数——字符串分割、字符串填充等
捡起MATLAB的第(5)天
Temporal model: long-term and short-term memory network (LSTM)
MySQL - MySQL查询语句的执行过程
5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
dlopen/dlsym/dlclose的简单用法
【第5节 if和for】
单体架构系统重新架构
CVPR 2022 quality paper sharing
Go concurrency and channel
gps北斗高精度卫星时间同步系统应用案例
Upgrade MySQL 5.1 to 5.610
Win11/10家庭版禁用Edge的inprivate浏览功能
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
捡起MATLAB的第(7)天
安装Redis并部署Redis高可用集群
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years