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 ...
随机推荐
- --投资情况统计详情sql
--投资情况统计详情sqlselect BidRecord.*, RegInfo.UserName,UserInfo.phone,BorrowInfo.Title,BorrowInfo.BorrowC ...
- bzoj 2245: [SDOI2011]工作安排
#include<cstdio> #include<iostream> #include<cstring> #define M 10000 #define inf ...
- soap
sudo apt-get update apt-get install php-soapphp-config --configure-options --enable-soap php -i | gr ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- SQLite实现Top功能
SQlite本身没有top功能,无法向TSQL一样下Select top 100 * from tb_table,但SQLite提供了一个Limit关键字用来取得相应行数的资料 具体语法实例:Sele ...
- C++封装、继承、多态
C++封装继承多态总结 面向对象的三个基本特征 面向对象的三个基本特征是:封装.继承.多态.其中,封装可以隐藏实现细节,使得代码模块化:继承可以扩展已存在的代码模块(类):它们的目的都是为了--代码重 ...
- ajax异步文件上传,iframe方式
不是我写的,我看了他的,思路很明确: 实现思路: 在js脚本中动态创建form,动态创建form中的内容,将文件上传的内容以隐藏域的方式提交过去,然后写好回调等. 感觉思路不难,但是我写不出来,感觉需 ...
- SharePoint 2013 开发——开发并部署第一个APP
博客地址:http://blog.csdn.net/FoxDave 本篇我们开始对开发APP应用程序进行了解. 本篇基于本地SharePoint环境(如果是Office 365的话会方便许多),需 ...
- 第四课 Gallery的使用
直接上代码 1.Layout--Main.axml <?xml version="1.0" encoding="utf-8"?> <Linea ...
- poj2955 区间dp
//Accepted 200 KB 63 ms //区间dp //dp[i][j] 从i位到j位能得到的最大匹配数 //dp[i][j]=max(dp[i+1][j-1] (s[i-1]==s[j-1 ...