415. Add Strings

Source: Internet
Author: User

The simplest part of the Bitinteger

Put two strings in turn, from back to forward, the place to learn is that for while conditions can be set to,index1< Len1 | | Index2 < Len2.

The Index1 < len1 and index2 <len2 are then processed internally, so there is no need to write a separate loop for one of the early end cases.

1      Publicstring AddStrings (String num1, String num2) {2         if(NUM1 = =NULL) {3             returnnum2;4         }5         if(num2 = =NULL) {6             returnNUM1;7         }8StringBuilder SB1 =NewStringBuilder (NUM1);9StringBuilder SB2 =NewStringBuilder (num2);Ten         intLen1 =num1.length (); One         intLen2 =num2.length (); A sb1.reverse (); - sb2.reverse (); -StringBuilder SB =NewStringBuilder (); the         intindex1 = 0; -         intIndex2 = 0; -         intCarry = 0; -          while(Index1 < Len1 | | Index2 <len2) { +             intCur =carry; -             if(Index1 <len1) { +Cur + = Sb1.charat (index1++)-' 0 '; A             } at             if(Index2 <len2) { -Cur + = Sb2.charat (index2++)-' 0 '; -             } -             if(Cur > 9) { -Cur%= 10; -Carry = 1; in}Else { -Carry = 0; to             } + sb.append (cur); -         } the         if(Carry = = 1) { *Sb.append (1); $         }Panax Notoginseng         returnsb.reverse (). toString (); -}

415. Add Strings

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.