Skip to content

299. Bulls and Cows #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
namespace-io opened this issue Aug 3, 2019 · 0 comments
Open

299. Bulls and Cows #334

namespace-io opened this issue Aug 3, 2019 · 0 comments
Labels

Comments

@namespace-io
Copy link
Owner

分几次查

class Solution {
public:
    string getHint(string secret, string guess) {
        int a[10] = {0};
        int ca, cb;
        ca = cb = 0;
        for(int i = 0; i < secret.size(); i++){
            a[secret[i]-'0']++;
        }
        
        for(int i = 0; i < guess.size(); i++){
            if(guess[i] == secret[i]) {
                ca++;
                a[secret[i] - '0']--;
            } 
        }
        
        for(int i = 0; i < guess.size(); i++ ){
            if(guess[i] != secret[i] && a[guess[i]-'0'] > 0){
                cb++;
                a[guess[i]-'0']--;
            }
        }
        
        return to_string(ca) + "A" + to_string(cb) + "B";
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant