Bulls and Cows 带测试版

来源:互联网 发布:程序员的英语单词 编辑:程序博客网 时间:2024/05/21 11:01
import java.util.*;public class Solution299 {    public static String getHint(String secret, String guess)     {         char[] se = secret.toCharArray();        char[] gu = guess.toCharArray();        int len = se.length;        int bull = 0;        int cow = 0;        Map<Character, Integer> seHas = new HashMap<haracter, Integer>();        for (int i = 0; i < len; i++) {            if (!seHas.containsKey(se[i])) {                seHas.put(se[i], 1);            } else {                seHas.put(se[i], seHas.get(se[i]) + 1);            }        }        Boolean[] b = new Boolean[len];        for (int i = 0; i < len; i++) {            if (se[i] == gu[i]) {                b[i]  = true;                seHas.put(gu[i], seHas.get(gu[i])-1);                cow++;            }else {                b[i] = false;            }        }        for (int j = 0; j < len; j++) {            if(b[j] == false && seHas.get(gu[j])!=null && seHas.get(gu[j])>0) {                bull ++;                seHas.put(gu[j], seHas.get(gu[j])-1);            }        }        return cow + "A" + bull + "B";    }    public static void main(String[]args)    {        System.out.println(getHint("1807","7810"));     }}
0 0
原创粉丝点击