您的位置:首页 > 编程语言 > Java开发

【Leetcode】299-bulls-and-cows【Java实现】【hash table】

2015-11-05 17:48 393 查看
最近有空就刷一下leetcode,299题自己的方法居然要50ms+,都不好意思贴出来。在discussion中看到了一个hash的方法。学习了一下,很巧妙,加油吧!

stem:

You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it. Each time your friend guesses a number, you give a hint. The hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"). Your friend will use those hints to find out 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.


code:
public class Solution {

    public String getHint(String secret, String guess) {
        int len=secret.length();
        
        int A=0;
        int B=0;
        int count[]=new int[10];

        for(int i=0;i<len;i++){
            int s=secret.charAt(i)-'0';
            int g=guess.charAt(i)-'0';
            if(s==g){
                A++;
            }else{ 
                if(count[g]>0){  //如果g这里之前多出一个和g匹配的则B加1
                    B++;
                }
                
                if(count[s]<0){ //如果之前少一个和s匹配的,则B加1
                    B++;
                }
                
                count[g]--;     //缺一个和g匹配的
                count[s]++;     //s这里多一个匹配的
            }
        }
        
        return A+"A"+B+"B";
    }
}


也贴一下自己的代码吧。。。虽然真的很挫。。。

public class Solution {
 
    public String getHint(String secret, String guess) {
        int len=secret.length();
        
        if(len==0)  //do not forget special situation
            return "0A0B";
        
        int A=0;
        int B=0;
        int flagA[]=new int[len];

        for(int i=0;i<len;i++){
            char s=secret.charAt(i);
            char g=guess.charAt(i);
            if(s==g){
                A++;
            }else{ 
                for(int j=0;j<len;j++){
                    char sj=secret.charAt(j);
                    char gj=guess.charAt(j);
                    if(g==sj && sj!=gj && flagA[j]!=1){
                         B++;
                         flagA[j]=1;
                         break;
                    }
                }
            }
        }
        
        return A+"A"+B+"B";
    }
}
完全没有技术含量可言。见笑。做了几道leetcode的题,发现hash是一个好方法,但感觉自己还没有抓住精髓。

这道题开始理解题意有问题,要注意,计算bull和cow的一一对应关系。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: