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. 使用 jQuery Deferred 和 Promise 创建响应式应用程序

    这篇文章,我们一起探索一下 JavaScript 中的 Deferred 和 Promise 的概念,它们是 JavaScript 工具包(如Dojo和MochiKit)中非常重要的一个功能,最近也首 ...

  2. 常用<meta>标签

    页面关键词 <meta name="keywords" content="your tags" /> 页面描述 <meta name=&quo ...

  3. c# Task编程一个task抛出异常后怎么取消其他线程

    从MSDN的Forum上看到别人提供的解决方案,感觉还是比较靠谱,所以就保存下来. CancellationTokenSource cts = new CancellationTokenSource( ...

  4. zip格式jdk在Linux环境下的安装过程

    1下载jdk [root@localhost opt]# cd soft_bak/[root@localhost soft_bak]# lsjdk1.7.0_25.zip postgresql-9.4 ...

  5. JavaScript——DOM操作——Window.document对象

    一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunment.getElementById(&qu ...

  6. CentOS 7 安装Dukto(局域网通信工具)

    rmp包 http://download.opensuse.org/repositories/home:/colomboem/CentOS_7/x86_64/dukto-6.0-13.1.x86_64 ...

  7. websotrm注册码

    webStorm : UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA ...

  8. [转]Apache Maven 入门篇(下)

    原文地址: Apache Maven 入门篇(下) 作者:George Ma 第一篇文章大概的介绍了一下Apache Maven以及它的下载和安装,并且运行了一个简单的示例.那么在对maven有了一点 ...

  9. 。。。Hibernate注解配置的注意事项。。。

    今天本来打算录视频的,突然遇到一个拦路虎,Hibernate注解配置,有一个注意点:要么都在属性上面注解配置,要么都在getXX()方法上面用注解配置,要不然就会报错: Caused by: org. ...

  10. SQL Server练习

    SQL Server 基本语法: http://www.w3school.com.cn/sql/sql_intro.asp 练习1: 运行语句: USE [Test1] select FNumber, ...