当前位置:网站首页>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
边栏推荐
- TIA botu - basic operation
- Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
- Matplotlib tutorial 05 --- operating images
- String sorting
- JS regular détermine si le nom de domaine ou le chemin de port IP est correct
- 捡起MATLAB的第(6)天
- Date date calculation in shell script
- js正则判断域名或者IP的端口路径是否正确
- utils. Deprecated in35 may be cancelled due to upgrade. What should I do
- Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
猜你喜欢
建设星际计算网络的愿景
OAK-D树莓派点云项目【附详细代码】
Distinct use of spark operator
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
[key points of final review of modern electronic assembly]
Best practices of Apache APIs IX high availability configuration center based on tidb
贫困的无网地区怎么有钱建设网络?
Ice -- source code analysis
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
Day (9) of picking up matlab
随机推荐
Es common query, sorting and aggregation statements
Accumulation of applet knowledge points
homwbrew安装、常用命令以及安装路径
Best practices of Apache APIs IX high availability configuration center based on tidb
Construction of esp32 compilation environment
Day (5) of picking up matlab
The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?
捡起MATLAB的第(9)天
Basic greedy summary
What is the experience of using prophet, an open source research tool?
C language self compiled string processing function - string segmentation, string filling, etc
String sorting
【现代电子装联期末复习要点】
[section 5 if and for]
Groupby use of spark operator
One brush 313 sword finger offer 06 Print linked list from end to end (E)
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
Merging of Shanzhai version [i]
OAK-D树莓派点云项目【附详细代码】