当前位置:网站首页>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
jExists 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
边栏推荐
- GRBL学习(一)
- C, calculation method and source program of bell number
- What is the experience of using prophet, an open source research tool?
- Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
- shell_ two
- Vim使用Vundle安装代码补全插件(YouCompleteMe)
- volatile的含义以及用法
- 运维流程有多重要,听说一年能省下200万?
- ES常用查询、排序、聚合语句
- Large factory technology implementation | industry solution series tutorials
猜你喜欢

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

Pgpool II 4.3 Chinese Manual - introductory tutorial
![[key points of final review of modern electronic assembly]](/img/17/007ca1f4fedd11ab878bddda3faf56.jpg)
[key points of final review of modern electronic assembly]

捡起MATLAB的第(7)天

捡起MATLAB的第(5)天

腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上

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

Distinct use of spark operator

MySQL - MySQL查询语句的执行过程

安装Redis并部署Redis高可用集群
随机推荐
Spark 算子之partitionBy
pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
Day (8) of picking up matlab
shell_ two
Partitionby of spark operator
MySQL - execution process of MySQL query statement
MySQL - MySQL查询语句的执行过程
matplotlib教程05---操作图像
【现代电子装联期末复习要点】
运维流程有多重要,听说一年能省下200万?
安装Redis并部署Redis高可用集群
Intersection, union and difference sets of spark operators
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
Nanny Anaconda installation tutorial
Fastjon2他来了,性能显著提升,还能再战十年
Sortby use of spark operator
OAK-D树莓派点云项目【附详细代码】
捡起MATLAB的第(9)天
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
linux上启动oracle服务