当前位置:网站首页>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
边栏推荐
- Date date calculation in shell script
- Simple usage of dlopen / dlsym / dlclose
- Cap theorem
- 糖尿病眼底病变综述概要记录
- 捡起MATLAB的第(9)天
- TIA botu - basic operation
- Intersection, union and difference sets of spark operators
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
- Distinct use of spark operator
- Config learning notes component
猜你喜欢

Method 2 of drawing ROC curve in R language: proc package

实现缺省页面

下载并安装MongoDB

Distinct use of spark operator

王启亨谈Web3.0与价值互联网“通证交换”

Multi level cache usage

Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm

IronPDF for .NET 2022.4.5455

Timing model: gated cyclic unit network (Gru)

Best practices of Apache APIs IX high availability configuration center based on tidb
随机推荐
Compile, connect -- Notes
Meaning and usage of volatile
Large factory technology implementation | industry solution series tutorials
TIA博图——基本操作
安装Redis并部署Redis高可用集群
Date date calculation in shell script
Codejock Suite Pro v20. three
IronPDF for .NET 2022.4.5455
One brush 313 sword finger offer 06 Print linked list from end to end (E)
Leetcode-374 guess the size of the number
ESP32_Arduino
gps北斗高精度卫星时间同步系统应用案例
Do we media make money now? After reading this article, you will understand
linux上启动oracle服务
Upgrade MySQL 5.1 to 5.610
Construction of esp32 compilation environment
The length of the last word of the string
捡起MATLAB的第(9)天
Go language, array, pointer, structure
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile