【leetcode】299. 猜数字游戏

 

char * getHint(char * secret, char * guess){
    int shash[10]={0};
    int ghash[1000]={0};
    int i, len=strlen(secret), A=0, B=0, l=0;
    char* s=(char*)calloc(10,sizeof(char));
    for (i=0; i<len; i++)
    {
        shash[secret[i]-'0']++;
        if(secret[i]==guess[i]){
            shash[secret[i]-'0']--;
            ghash[i]++;
            A++;
        }
    }
    for (i=0; i<len; i++)
    {
        if(ghash[i])
            continue;
        if(shash[guess[i]-'0']>0){
            shash[guess[i]-'0']--;
            B++;
        }
    }
    sprintf(s,"%dA%dB",A,B);
    return s;
}

 

posted @ 2020-12-31 20:54  温暖了寂寞  阅读(96)  评论(0编辑  收藏  举报