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