4 ms runtime for C++


  • 0
    H
        int j = 0; //counter
        int b = 0, cow = 0; //Number of bull
        int c[10] = {0};
        int totals[1000] = {0};
        for (auto g : guess) {
            c[g - '0']++;
        }
        for (auto s : secret) {
            if (s == guess[j]) {
                b++;
                c[s - '0']--;
                totals[j] = 1;
            }
            j++;
            
        }
        j = 0;
        for (auto s : secret) {
            if (c[s-'0'] > 0 & totals[j++] == 0) {
                cow++;
                c[s-'0'] --;
            }
        }
        return to_string(b) + 'A' + to_string(cow) + 'B';

  • 0
    H

    What is totals for? Will the answer be correct without totals? Thanks!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.