Leetcode--DAY13

Source: Internet
Author: User
Tags switch case

Question 1ADD Binary

Given binary strings, return their sum (also a binary string).

For example,
A ="11"
b ="1"
Return "100" .

Firslty I Wirte A very direct and kind of brute Forch method. Just consider each case of the sum 1, 0, 2, 3 for Anum + Bnum + carry by switch case.

1  Public classSolution {2     3String result = "";4     intCarry = 0;5     6      Publicstring Addbinary (String A, string b) {7         if(a.length () = = 0 )8             returnb;9         Else if(b.length () = = 0)Ten             returnA; One          A          -         intAlen = A.length ()-1; -         intBlen = B.length ()-1; the          -          -          while(Alen >= 0 && Blen >=0){ -             intAnum = A.charat (alen)-' 0 '; +             intBnum = B.charat (blen)-' 0 '; -Calculator (Anum + bnum +carry); +Alen--; ABlen--; at         } -          -          while(Alen >= 0){ -             intAnum = A.charat (alen)-' 0 '; -Calculator (Anum +carry); -Alen--; in         } -          to          while(Blen >= 0){ +             intBnum = B.charat (blen)-' 0 '; -Calculator (Bnum +carry); theBlen--; *         } $         Panax Notoginseng         if(Carry = = 1) -result = "1" +result; the          +         returnresult; A     } the      +      Public voidCalculatorintsum) { -         Switch(sum) { $              Case0: $result = "0" +result; -                  Break; -              Case1: theresult = "1" +result; -Carry = 0;Wuyi                  Break; the              Case2: -result = "0" +result; WuCarry = 1; -                  Break; About              Case3: $result = "1" +result; -Carry = 1; -                  Break; -         } A     } +}

The other-the-does not-use-switch case but use%2 OR/2-defind carry and Sum, which is much clear and shorter.

1  Publicstring Addbinary (String A, string b) {2         if(a.length () = = 0 )3             returnb;4         Else if(b.length () = = 0)5             returnA;6             7String result = "";8         intCarry = 0;9         Ten         intLen =Math.max (A.length (), b.length ()); One          A          for(inti = 0; i < Len; i + +){ -             intAnum = 0; -             intBnum = 0; the             if(I <a.length ()) -Anum = A.charat (A.length ()-1-i)-' 0 '; -             if(I <b.length ()) -Bnum = B.charat (B.length ()-1-i)-' 0 '; +              -             intsum = anum + Bnum +carry; +result = sum%2 +result; Acarry = SUM/2; at         } -          -         if(Carry = = 1) -result = "1" +result; -          -         returnresult; in          -}

Leetcode--DAY13

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.