67. Add Binary
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的更多相关文章
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
- 【LeetCode】67. Add Binary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...
- LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- 【LeetCode】67. Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...
- 【一天一道LeetCode】#67. Add Binary
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
随机推荐
- POJ 2739 Sum of Consecutive Prime Numbers 难度:0
题目链接:http://poj.org/problem?id=2739 #include <cstdio> #include <cstring> using namespace ...
- MONGODB 查询
1,mongoDB 取模运算:db.person.find({index:{$mod:[5,1]}})db.person.find({index:{$not:{$mod:[5,1]}}})2,name ...
- javascript 错误处理
try{ var date=new Date(); date.test();//调用date的未定义的test方法; document.wrire("try块执行结束<br>&q ...
- HDU5478 原根求解
看别人做的很简单我也不知道是怎么写出来的 自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b那么只要求出幂a,b就能得到所求值a,b 自己慢慢化简就会发现可以抵消n然 ...
- JS constructor
1.每个对象都有一个constructor,都指向创建该对象的方法. 2.function对象的prototype的constructor也是指向该方法.如果对prototype进行重写,constr ...
- 微软TechEd2013大会门票热卖!
微软TechEd2013大会将在北京.上海两地隆重举行! 会议时间安排如下: 北京:12月5日—6日 国家会议中心 上海:12月11日—12日 国际会议中心 现在是门票热卖时期,票价:2688.0 ...
- Makefile学习笔记
ls -l 查看文件详细信息 1.gcc -E test.c -o test.i//预编译gedit test.i //查看:高级C 2.gcc -Wall -S test.i -o test.s// ...
- 《View Programming Guide for iOS》之frame、bounds和center之间的关系
The frame property contains the frame rectangle, which specifies the size and location of the view i ...
- 【LEETCODE OJ】Binary Tree Preorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...
- ERP通用存储过程封装(三)
一:解释 SQL Server提供了一种方法:可以将一些预先编译的SQL语句集中起来由SQL Server数据库服务器来完成以实现某个任务,这就是存储过程.存储过程常驻在SQL Server ...