299. Bulls and Cows
299. Bulls and Cows刚开始没看懂题目,这个问题很简单的,就是要统计两个数组中,有哪些是位置对,值也对,有哪些是值对,位置不对,没什么难度
class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
A = B = 0
count_1 = [0] * 10
count_2 = [0] * 10
for i in range(len(secret)):
if secret[i] == guess[i]:
A += 1
else:
s = int(secret[i])
g = int(guess[i])
count_1[s] += 1
count_2[g] += 1
for i in range(10):
B += min(count_1[i], count_2[i])
return str(A) + "A" + str(B) + "B"
class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
A = B = 0
count = [0] * 10
for i in range(len(secret)):
if secret[i] == guess[i]:
A += 1
else:
s = int(secret[i])
g = int(guess[i])
if count[s] < 0:
B += 1
if count[g] > 0:
B += 1
count[s] += 1
count[g] -= 1
return str(A) + "A" + str(B) + "B"
网友评论