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. @propetry参数

    // //  Person.h //  01-手动内存管理基本概念 // //  Created by apple on 14-3-17. // // #import <Foundation/F ...

  2. Visual Studio Online

    删除Visual Studio Online的项目http://taslimi.me/how-to-delete-a-team-project-from-tfs-online-tfs.visualst ...

  3. openssl HeartBlood

    受影响[编辑] OpenSSL 1.0.2-beta OpenSSL 1.0.1 - OpenSSL 1.0.1f 除非针对CVE-2014-0160的操作系统补丁已经安装,而没有更改库版本,如Deb ...

  4. PDB重命名

    PDB重命名 将PDB clonedb重命名为rdb SQL> select name,open_mode from v$pdbs; NAME OPEN_MODE --------------- ...

  5. java 笔记(3) —— 动态代理,静态代理,cglib代理

    0.代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口. 代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等. 代理类与委托类之间通常会存 ...

  6. Azure 意外重启, 丢失sql server master表和 filezilla

    突然发现今晚网站打不开了,提示连不上数据库. ftp也连不上了. 远程连上Azure 发现机器意外重启, 丢失sql server master表和 filezilla 要重新安装. 又耗费我几个小时 ...

  7. 试用windows Azure

    试用windows Azure, 需要国外手机注册,信用卡注册. windows操作系统,只有2008R2,2012,2012R2可以选择,我选择XS最低档,然后选2012R2,欧洲数据中心,那个慢啊 ...

  8. Three.js基础探寻十——动画

    本篇将介绍如果使用Three.js进行动态画面的渲染.此外,将会介绍一个Three.js作者写的另外一个库stat.js,用来观测每秒帧数(FPS). 1.实现动画效果 1.1 动画原理 对于Thre ...

  9. java io读书笔记(3)数值类型的数据

    input stream读取字节:out stream写入字节.Readers读取字符而Writers写入字符.因此,如果我们想理解input和output,我们首先就要明白 java如何处理字节,整 ...

  10. Codeforce Round #217 Div2

    e,妈蛋,第二题被hack了 没理解清题意,- -居然也把pretest过了,- -# A: 呵呵! B:包含任意一个子集的输出NO!,其他输出YES! C:贪心额,类似上次的Topcoder的500 ...