299. Bulls and Cows via java

class Solution {
    public String getHint(String secret, String guess) {
        int bull = 0;
        int cow = 0;
        int[] mem = new int[10];
        for(int i =0; i<secret.length();i++){
            if(secret.charAt(i)==guess.charAt(i)){
                bull++;
            }else{
                if (mem[secret.charAt(i)-'0']++ < 0){
                    cow++;
                } 
                if (mem[guess.charAt(i)-'0']-- > 0){
                    cow++;
                }
            }
        }
        return bull+"A"+cow+"B";
    }
}

bulls & cows, 用数组保存见过的数字的次数

posted @ 2019-11-24 16:27  La_Campanella  阅读(95)  评论(0编辑  收藏  举报