[leetcode]Bulls and Cows

4710 ワード

今日はEasyの問題に多くの時間を費やしました.まず問題を出します.
Bulls and Cows Total Accepted: 7492 Total Submissions: 30491 Difficulty: Easy You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.
For example:
Secret number: “1807” Friend’s guess: “7810” Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.) Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return “1A3B”.
Please note that both secret number and friend’s guess may contain duplicate digits, for example:
Secret number: “1123” Friend’s guess: “0111” In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return “1A1B”. You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.
                     ,                      ,           :
  • 最初の理解は、友人が当てた数字はすべてcowと呼ぶことができるが、実際にはそうではない(この点は私のテーマがはっきりしていない)、例の(11230,111)というデータのセットのように、出力されたのは1 A 1 Bが1 A 2 Bではないので、プログラムの実現過程では、使用した数字の除去に注意しなければならない.
  • の間にWAが(1121222)というデータのセットの中で、私の出力は3 A 1 Bですが、出力を期待する3 A 0 Bは、bullsのレベルがcowより高いことを示しています.つまり、プログラムは遅いbullの個数を判断してからcowを求め、1サイクルで直接終わることはできません.
  • 配列方法を使っていたので(後で少し気分が悪くなった)、その間に配列の下付きを間違えて使っても何度もWAさせてもらいました.添付コード:
  • public class Solution {
        public String getHint(String secret, String guess) {
                int bullNum = 0, cowNum = 0, i;
                int num, num2;
                int inSecret[] = new int[10];
                boolean isAppear[] = new boolean[10];
                for(i = 0; i < 10; i++){
                    inSecret[i] = 0;
                    isAppear[i] = false;
                }
                for(i = 0; i < secret.length(); i++){
                    num = secret.charAt(i) - '0';
                    inSecret[num]++;
                    isAppear[num] = true;
                }
                for(i = 0; i < secret.length(); i++){
                   num2 = guess.charAt(i) - '0';
                   if(secret.charAt(i) == guess.charAt(i)){
                       bullNum ++;
                       inSecret[num2] --;
                   }
                }
                for(i = 0; i < secret.length(); i++){
                    if(secret.charAt(i) != guess.charAt(i)){
                        num2 = guess.charAt(i) - '0';
                         if(inSecret[num2] != 0 && isAppear[num2]){
                               cowNum ++;
                               inSecret[num2] --;
                          }
                    }
                }
    
                String res = bullNum + "" + "A" + cowNum + "" + "B";
                return res;
        }
    }
              ,               ,        。
    
        :(https://leetcode.com/problems/bulls-and-cows/)