[LeetCode-interview algorithm classic-Java implementation] [067-Add Binary (Binary addition)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [067-Add Binary (Binary addition)], leetcode -- java
[067-Add Binary (Binary addition )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

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

Theme

Given two binary strings, return their sum, which is also a binary string.

Solutions

First, convert the two binary strings to the corresponding integer array, and add them from the low position to the high position. At the same time, consider the situation that the last addition also needs to expand one bit. For more information, see code implementation.

Code Implementation

Algorithm Implementation class

Public class Solution {public String addBinary (String a, String B) {int [] ca = new int [. length ()]; int [] cb = new int [B. length ()]; // converts the value in the character array to 0 or 1 for (int I = 0; I <. length (); I ++) {ca [I] =. charAt (I)-'0';} // converts the value in the character array to 0 or 1 for (int I = 0; I <B. length (); I ++) {cb [I] = B. charAt (I)-'0';} // if (ca. length <cb. length) {int [] tmp = ca; ca = cb; cb = tmp;} int ai = ca. length-1; // character Array ca's last index subscript int bi = cb. length-1; // The character array cb's last index subscript int carry = 0; // The lower-bit carry mark int result; // The loaded result // calculation example: 1010101101 + 10100 while (ai> = 0 & bi> = 0) {result = ca [ai] + cb [bi] + carry; ca [ai] = result % 2; carry = result/2; ai --; bi --;} // process the remaining number while (ai> = 0) {result = ca [ai] + carry; ca [ai] = result % 2; carry = result/2; if (carry = 0) {break;} ai --;} // convert the value in the character array to 0 or 1 for (int I = 0; I <ca. length; I ++) {ca [I] + = '0';} // you do not need to extend an if (carry = 0) {char [] ch = new char [ca. length]; for (int I = 0; I <ca. length; I ++) {ch [I] = (char) (ca [I]);} return new String (ch );} // an else {char [] ch = new char [ca. length + 1]; ch [0] = '1'; for (int I = 0; I <ca. length; I ++) {ch [I + 1] = (char) (ca [I]);} return new String (ch );}}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the source for reprinting [http://blog.csdn.net/derrantcm/article/details/47203323]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.