package addBinary67;
/*
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
*/
public class Solution {
public static String addBinary(String a, String b) {
//ensure a.length()<=b.length()
if (a.length()>b.length())
return addBinary(b,a);
char[] chara=a.toCharArray();
char[] charb=b.toCharArray();
int lena=chara.length;
int lenb=charb.length;
StringBuilder sb=new StringBuilder();
int carry=0;
//add the same length numbers
for (int i=0;i<lena;i++){
int inta=chara[lena-1-i]-'0';
int intb=charb[lenb-1-i]-'0';
sb.append(inta^intb^carry);
carry=(inta&intb)|((inta^intb)&carry);
}
//add the longer length numbers
for (int i=lenb-lena-1;i>=0;i--){
int intb=charb[i]-'0';
sb.append(intb^carry);
carry=intb&carry;
}
//judge the first bit
if(carry>0)
sb.append(carry);
return sb.reverse().toString();

}
public static void main(String[] args) {
// TODO Auto-generated method stub
String a="10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101";
String b="110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011";
//String a="101";
//String b="10111";
System.out.println(addBinary(a,b));
//110111101100010011000101110110100000011101000101011001000011011000001100011110011010010011000000000

}

}

LeetCode----67. Add Binary(java)的更多相关文章

  1. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  2. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  3. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  4. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  5. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  6. [leetcode]67. Add Binary 二进制加法

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  7. LeetCode - 67. Add Binary(4ms)

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  8. leetcode 67. Add Binary (高精度加法)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  9. LeetCode 67 Add Binary(二进制相加)(*)

    翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...

  10. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

随机推荐

  1. ADO.Net 数据库访问技术

    1.在web.Config配置文件中设置连接字符串 <connectionStrings> <add name="connString" connectionSt ...

  2. iOS SQLite 增删改查的封装(关系型)

    在工程里导入libsqlite3.tbd库(Xcode 7) #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder &l ...

  3. UICollectionView未充满时也可以滚动

    当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的 self.Cov.alway ...

  4. Hadoop学习(2)-- HDFS

    随着信息技术的高度发展,数据量越来越多,当一个操作系统管辖范围存储不下时,只能将数据分配到更多的磁盘中存储,但是数据分散在多台磁盘上非常不方便管理和维护,迫切需要一种系统来管理多台机器上的文件,因此诞 ...

  5. javascript 正则表达式基础知识汇总

    正则表达式:对字符串中的信息实现查找.替换和提取操作.(不支持注释和空白,必须写在一行内)正则表达式的创建:包含在一对斜杠之间的字符(直接量语法)例如:var pattern = /s$/;     ...

  6. FlashBuilder 4.7 win64 和 mac版 下载地址

    win64 http://trials3.adobe.com/AdobeProducts/FLBR/4_7/win64/FlashBuilder_4_7_LS10_win64.exe mac http ...

  7. 【Origin】 偶题 之 抒意

    作词抒胸臆, 辞赋九万里: 从南盖到北, 句句表挚期. -作于二零一五年七月十五日

  8. monkey测试(转)

    一.Monkey测试简介Monkey测试是Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕.滑动Trackball.按键等操作来对设备上的程序进行压力测试,检测程序多久的时 ...

  9. bzoj 4237稻草人

    按x轴进行分治,将[l,r]分成[l,mid]和[mid+1,r],左下角点x值在[l,mid]中,右上角点x值在[mid+1,r],然后将[l,r]中的所有点按y轴排序,按顺序扫描,若扫描到左下角点 ...

  10. hdu5314 Happy King

    树分治. 代码 #pragma comment(linker, "/STACK:102400000,102400000") #include<cstdio> #incl ...