every blog every motto: You will never know unless you try
0. 前言
生活好难,再坚持坚持!
1. 字符串
1.1 题目
1.2
1.2.1 方法一:
class Solution:
def getHint(self, secret: str, guess: str) -> str:
bulls = 0
cows = 0
bulls_cows = 0
for i in range(len(secret)):
if secret[i] == guess[i]:
bulls += 1
unqiue = set(guess)
for ele in unqiue:
if ele in secret:
bulls_cows += min(guess.count(ele), secret.count(ele))
cows = bulls_cows-bulls
return str(bulls)+'A'+str(cows)+'B'
1.2.2 方法二:
class Solution:
def getHint(self, secret: str, guess: str) -> str:
secret_count = [0] * 10
guess_count = [0] * 10
bulls = 0
cows = 0
for index in range(len(guess)):
if secret[index] == guess[index]:
bulls += 1
else:
secret_count[ord(secret[index]) - ord('0')] += 1
guess_count[ord(guess[index]) - ord('0')] += 1
for index in range(10):
cows += min(secret_count[index],guess_count[index])
return str(bulls)+'A'+str(cows)+'B'
1.2.3
class Solution:
def getHint(self, secret: str, guess: str) -> str:
unmatch_count = [0] * 10
bulls = 0
cows = 0
for index in range(len(guess)):
if secret[index] == guess[index]:
bulls += 1
else:
if unmatch_count[ord(secret[index]) - ord('0')] < 0:
cows += 1
unmatch_count[ord(secret[index]) - ord('0')] += 1
if unmatch_count[ord(guess[index]) - ord('0')] > 0:
cows += 1
unmatch_count[ord(guess[index]) - ord('0')] -= 1
return str(bulls) + 'A' + str(cows) + 'B'
|