Leetcode 299. Bulls and cows

Source: Internet
Author: User

Secret and guess are read in bits, and if equal bull is added 1, the inequality is in the array of each number of different numbers in which they are counted, plus 1 (s_map[i] refers to the number of numbers I appear in secret). The last S_map and G_map are the number of occurrences of each value in the respective string, cow equals the smallest of the same position in two arrays.

1 classSolution {2  Public:3     stringGethint (stringSecretstringguess) {4         ints_map[Ten] = {0}, g_map[Ten] = {0};5         intBull =0, cow =0;6         7          for(inti =0; I < secret.length (); ++i) {8             if(Secret[i] = = Guess[i]) + +Bull;9             Else{Ten++s_map[secret[i]-'0']; One++g_map[guess[i]-'0']; A             } -         } -          the          for(inti =0; I <Ten; ++i) { -Cow + =min (s_map[i], g_map[i]); -         } -          +         -         stringres =""; +res = to_string (Bull) +"A"+ to_string (Cow) +"B"; A         returnRes; at     } -};

Leetcode 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.