当前位置:网站首页>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
边栏推荐
- vim指定行注释和解注释
- 5 minutes, turn your excel into an online database, the magic cube net table Excel database
- 多级缓存使用
- Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
- s16. One click installation of containerd script based on image warehouse
- Spark 算子之交集、并集、差集
- Best practices of Apache APIs IX high availability configuration center based on tidb
- Go concurrency and channel
- Named in pytoch_ parameters、named_ children、named_ Modules function
- 贫困的无网地区怎么有钱建设网络?
猜你喜欢

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

贫困的无网地区怎么有钱建设网络?

捡起MATLAB的第(6)天

捡起MATLAB的第(9)天

Coalesce and repartition of spark operators

Website pressure measurement tools Apache AB, webbench, Apache jemeter

5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库

Construction of esp32 compilation environment

WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile

糖尿病眼底病变综述概要记录
随机推荐
Nanny Anaconda installation tutorial
Why disable foreign key constraints
王启亨谈Web3.0与价值互联网“通证交换”
MySQL - execution process of MySQL query statement
C#,贝尔数(Bell Number)的计算方法与源程序
Large factory technology implementation | industry solution series tutorials
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
[section 5 if and for]
Deletes the least frequently occurring character in the string
Go language, condition, loop, function
Matplotlib tutorial 05 --- operating images
Day (8) of picking up matlab
Day (7) of picking up matlab
How important is the operation and maintenance process? I heard it can save 2 million a year?
糖尿病眼底病变综述概要记录
Spark 算子之filter使用
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
Spark 算子之coalesce与repartition
Pgpool II 4.3 Chinese Manual - introductory tutorial
Interview questions of a blue team of Beijing Information Protection Network