当前位置:网站首页>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
边栏推荐
- 建设星际计算网络的愿景
- C language --- advanced pointer
- Temporal model: long-term and short-term memory network (LSTM)
- IronPDF for . NET 2022.4.5455
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
- JVM - Chapter 2 - class loader subsystem
- Go language, condition, loop, function
- Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
- The length of the last word of the string
- 多线程原理和常用方法以及Thread和Runnable的区别
猜你喜欢
C language self compiled string processing function - string segmentation, string filling, etc
多线程原理和常用方法以及Thread和Runnable的区别
Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
保姆级Anaconda安装教程
Do we media make money now? After reading this article, you will understand
Best practices of Apache APIs IX high availability configuration center based on tidb
Load Balancer
Spark 算子之filter使用
Spark 算子之partitionBy
Vision of building interstellar computing network
随机推荐
撿起MATLAB的第(9)天
js正則判斷域名或者IP的端口路徑是否正確
Spark 算子之partitionBy
shell_2
gps北斗高精度卫星时间同步系统应用案例
Go language, array, pointer, structure
多线程原理和常用方法以及Thread和Runnable的区别
Application of Bloom filter in 100 million flow e-commerce system
Leetcode-374 guess the size of the number
捡起MATLAB的第(4)天
Named in pytoch_ parameters、named_ children、named_ Modules function
IronPDF for . NET 2022.4.5455
How do you think the fund is REITs? Is it safe to buy the fund through the bank
matplotlib教程05---操作图像
Coalesce and repartition of spark operators
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
js正则判断域名或者IP的端口路径是否正确
pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
下载并安装MongoDB