299. Bulls and cows

Source: Internet
Author: User

    /** 299. Bulls and Cows * 2016-7-2 by Mingyang * code is ugly, but this topic is too simple, not details * Special case 1122-"1222 * guess the first 2 in the show will let me in the CO W plus 1 * So I'm going to go through the equal so it won't be wrong, but the code can be modified to a lot of optimizations, there's time to write slowly*/     Public Staticstring Gethint (String secret, String guess) {intBull = 0; intCow = 0; HashSet<Character> set =NewHashset<character>(); int[] Array =New int[10];  for(inti = 0; I < secret.length (); i++) {            CharA =Secret.charat (i); Array[a-' 0 ']++;        Set.add (a); }         for(inti = 0; I < guess.length (); i++) {            Charb =Guess.charat (i); if(Guess.charat (i) = =Secret.charat (i)) {Bull++; Array[b-' 0 ']--; Continue; }        }         for(inti = 0; I < guess.length (); i++) {            Charb =Guess.charat (i); if(Set.contains (b) && Guess.charat (i)! =Secret.charat (i)) {                if(Array[b-' 0 ') > 0) {Cow++; Array[b-' 0 ']--; }}} String res= Bull + "A" + Cow + "B"; returnRes; }

299. Bulls and cows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.