标签:out bsp Plan ++ string san col hint output
Example 1:
Input: secret = "1807", guess = "7810" Output: "1A3B" Explanation:1
bull and3
cows. The bull is8
, the cows are0
,1
and7.
Example 2:
Input: secret = "1123", guess = "0111" Output: "1A1B" Explanation: The 1st1
in friend‘s guess is a bull, the 2nd or 3rd1
is a cow.
class Solution { public String getHint(String secret, String guess) { int[] nums = new int[10]; int len = secret.length(); int bulls = 0; int cows = 0; for (int i = 0; i < len; i++) { char sWord = secret.charAt(i); char gWord = guess.charAt(i); if (sWord == gWord) { bulls += 1; } else { if (nums[gWord - ‘0‘] > 0) { cows += 1; } if (nums[sWord - ‘0‘] < 0) { cows += 1; } nums[sWord - ‘0‘] += 1; nums[gWord - ‘0‘] -= 1; } } return bulls + "A" + cows + "B"; } }
标签:out bsp Plan ++ string san col hint output
原文地址:https://www.cnblogs.com/xuanlu/p/12301038.html