[Java][LeetCode][HashMap] Bulls and Cows

SP Hou
May 16, 2022

--

最近又接了一個案子小忙,有點拖~但還是每天都會刷一題,只是不一定有時間寫紀錄,還是會寫,只是慢了一點~

這題就是我們小時候玩的1A2B遊戲,go through一遍secret和guess的全部元素,就可以找出A的部分,同時也在secret和guess同位置但不同字元時,將secret的該字元計數。接下來歷遍guess,並將有出現在map中的元素做遞減同時計數B,當遞減到0的時候,就把這個元素移除。

Traverse element of secret and guess at the same time. The count will be pulsed one when the guess character is the same as the secret character at the same index. If the secret and guess character are different, the character will be put in map and count the number. The next step is traverse elements of the guess string. If the character is appeared in map, I will subtract the count of character and I will count B number at the same time. Until the count is 0, then remove the character in the map.

--

--

No responses yet