"Leetcode-Interview algorithm classic-java implementation" "067-add binary (binary addition)"

Source: Internet
Author: User

"067-add binary (binary addition)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Given binary strings, return their sum (also a binary string).
For example,
A = "11"
b = "1"
Return "100"

Main Topic

Given two binary strings, they are returned and are also binary strings.

Thinking of solving problems

The corresponding two binary strings are converted to the corresponding integer array, from low to high to add, but also to consider the last addition to expand a bit of the case. For details, see Code implementation.

Code Implementation

Algorithm implementation class

 Public classSolution { PublicStringaddbinary(String A, string b) {int[] CA =New int[A.length ()];int[] cb =New int[B.length ()];//Converts the values in the character array to 0 or 1 of the value         for(inti =0; I < a.length (); i++) {Ca[i] = A.charat (i)-' 0 '; }//Converts the values in the character array to 0 or 1 of the value         for(inti =0; I < b.length (); i++) {Cb[i] = B.charat (i)-' 0 '; }//Long length saved with CA        if(Ca.length < Cb.length) {int[] tmp = CA;            CA = CB;        CB = TMP; }intAi = ca.length-1;//character array CA last index subscript        intBI = cb.length-1;//character array CB last index subscript        intcarry =0;//Inferior carry indicator        intResult//Load Results        //calculation such as: 1010101101 + 10100         while(Ai >=0&& Bi >=0{result = Ca[ai] + Cb[bi] + carry; Ca[ai] = result%2; Carry = result/2;            ai--;        bi--; }//Processing the remaining figures         while(Ai >=0) {result = Ca[ai] + carry; Ca[ai] = result%2; Carry = result/2;if(Carry = =0) { Break;        } ai--; }//Converts the value in the character array to 0 or 1 of the characters         for(inti =0; i < ca.length; i++) {Ca[i] + =' 0 '; }//Do not need to expand one        if(Carry = =0) {Char[] ch =New Char[Ca.length]; for(inti =0; i < ca.length; i++) {Ch[i] = (Char) (Ca[i]); }return NewString (CH); }//need to expand one        Else{Char[] ch =New Char[Ca.length +1]; ch[0] =' 1 '; for(inti =0; i < ca.length; i++) {ch[i +1] = (Char) (Ca[i]); }return NewString (CH); }    }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47203323"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java implementation" "067-add binary (binary addition)"

Related Article

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.