public class Solution {
public String addBinary(String a, String b) {
char[] aa=a.toCharArray();
char[] bb=b.toCharArray(); int size=aa.length>=bb.length?aa.length:bb.length;
int[] mm=new int[size];
int c=0;
int i=aa.length-1,j=bb.length-1,k=size-1;
for(;i>=0&&j>=0;i--,j--,k--)
{
mm[k]=(aa[i]-'0'+bb[j]-'0'+c)%2; if(aa[i]-'0'+bb[j]-'0'+c>=2) c=1;
else c=0;
} while(i>=0)
{
mm[k]=(aa[i]-'0'+c)%2;
if(aa[i]-'0'+c>=2) c=1;
else c=0; k--;i--; }
while(j>=0)
{
mm[k]=(bb[j]-'0'+c)%2; if(bb[j]-'0'+c>=2) c=1;
else c=0; k--;j--;
} String s="";
if(c==1)
s+=String.valueOf(c); for(int n=0;n<mm.length;n++)
s=s+String.valueOf(mm[n]); return s;
}
}

67. Add Binary的更多相关文章

  1. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. LeetCode练题——67. Add Binary

    1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...

  4. 【LeetCode】67. Add Binary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...

  5. LeetCode 67. Add Binary

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

  6. Java [Leetcode 67]Add Binary

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

  7. 【LeetCode】67. Add Binary

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

  8. 【一天一道LeetCode】#67. Add Binary

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  9. (String) leetcode 67. Add Binary

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

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

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

随机推荐

  1. strstr strchr strrchr

    通过函数的定义来区分: 1.strstr: 返回子串出现的第一次位置 char *strstr(const char *haystack, const char *needle) 可见,strstr函 ...

  2. DataGridView的按钮列的点击事件

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { ) { DataGr ...

  3. 使用ROS节点(五)

    先启动roscore roscore 为了获取节点信息,可以使用rosnode命令 $ rosnode 获取得一个可接受参数清单

  4. Visual studio 2013的安装和单元测试

    vs2013安装过程: 简单的单元测试: 1.创建新的c#类库 2.创建单元测试 3.测试结果 单元测试结束

  5. Deployment failed due to an error in FastDev assembly synchronization.

    在编译的时候发生Assembly synchronization error,显示信息为:Deployment failed due to an error in FastDev assembly s ...

  6. URAL 1306 Sequence Median(优先队列)

    题意:求一串数字里的中位数.内存为1M.每个数范围是0到2的31次方-1. 思路:很容易想到把数字全部读入,然后排序,但是会超内存.用计数排序但是数又太大.由于我们只需要第n/2.n/2+1大(n为偶 ...

  7. WIN中SharePoint Server 2010 入门安装部署详解

    目前流行的原始安装文件基本都是这样的:Windows Server 2008 R2+SQL Server 2008R2+SharePoint Server 2010     这个初始环境原本也无可厚非 ...

  8. DP重新学

    白书上的DP讲义:一 二 DAG上的dp 不要好高骛远去学这种高端东西,学了也写不对,剩下的几天把基本的dp和搜索搞下,就圆满了.不要再学新算法了,去九度把现有的算法写个痛. 学了数位DP和记忆搜索, ...

  9. (转)关于java和web项目中的相对路径问题

    原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...

  10. openstack context

    之前一直不知道context模块中存储的是什么东西,这回看一下代码: 其中最主要的类是:RequestContext: class RequestContext(object):   "&q ...