当前位置:网站首页>299. Number guessing game
299. Number guessing game
2022-04-23 16:03:00 【Zhang Joshua】
299. Guess the number game
- Question no : Power button 299
- Knowledge point : Hashtable , character string , Count
- Goal completion :23/150
- summary
stem :
Ideas :
- 1. General idea : Look for the ‘ Bull ’, Then delete the bull and find ‘ cow ’.
- 2. Concrete realization :
- (1) Put two strings into the dictionary , Build hash table .
- (2) First traversal , lookup ‘ Bull ’, That is, elements with the same index position and characters , When found, the... In the two hash tables will be value reduce 1, This step is a bit like Xiaole ?
- (3) Second traversal , lookup ‘ cow ’( In the last step, the bull has been eliminated ), If the element
j
Exists in both hash tables and value>0, It means it's a cow
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
- Reference resources : The simpler way to solve the problem
版权声明
本文为[Zhang Joshua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231558306204.html
边栏推荐
- 撿起MATLAB的第(9)天
- Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
- 5 minutes, turn your excel into an online database, the magic cube net table Excel database
- Intersection, union and difference sets of spark operators
- CVPR 2022 quality paper sharing
- Vim使用Vundle安装代码补全插件(YouCompleteMe)
- 运维流程有多重要,听说一年能省下200万?
- Ice -- source code analysis
- MySQL optimistic lock to solve concurrency conflict
- 安装Redis并部署Redis高可用集群
猜你喜欢
一文掌握vscode远程gdb调试
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
C语言自编字符串处理函数——字符串分割、字符串填充等
Do we media make money now? After reading this article, you will understand
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Day (6) of picking up matlab
Timing model: gated cyclic unit network (Gru)
糖尿病眼底病变综述概要记录
C language --- string + memory function
Spark 算子之sortBy使用
随机推荐
OAK-D树莓派点云项目【附详细代码】
linux上启动oracle服务
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
Do we media make money now? After reading this article, you will understand
MySQL optimistic lock to solve concurrency conflict
How can poor areas without networks have money to build networks?
VIM specifies the line comment and reconciliation comment
pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
如何进行应用安全测试(AST)
Win11/10家庭版禁用Edge的inprivate浏览功能
Day (2) of picking up matlab
Nanny Anaconda installation tutorial
Function summary of drawing object arrangement in R language
【第5节 if和for】
Es common query, sorting and aggregation statements
贫困的无网地区怎么有钱建设网络?
糖尿病眼底病变综述概要记录
Sortby use of spark operator
Start Oracle service on Linux