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 ...
随机推荐
- linux下安装vtune_amplifier_xe_2015_update4
说明: linux系统: CentOS 6.0 Vtune版本: 2015 安装过程: 1.下载vtune_amplifier_xe_2015_update4.tar.gz(到官网去下载 ...
- Countries in War (POJ 3114) Tarjan缩点+最短路
题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间. 解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...
- C# 子窗体点击按钮产生的新子窗体放在父窗体里
情景展示: 父窗体Form1,左边是按钮,右边是panel(放置子窗体) 父窗体点击按钮,在panel显示第一个子窗体AA, AA有个按钮,点击按钮,是第二个子窗体ZZ, 怎样将AA的子窗体ZZ也显示 ...
- 使用Myeclipse创建自定义签名debug keystore
1.在已经创建后的android项目上右击鼠标,如图所示 2.选择next下一步 3.选择create new keystore 注意 这里密码要输入android 4.点击next,录入基本信息 ...
- Android Studio 使用genymotion 模拟器运行app时 提示找不到任何设备
原因是使用了genymotion 默认的Android toos .打开genymotion 选择设置 ADB 使用自己的SDKtools 选择Android Studio 使用的SDK位置就行 ...
- SharePoint 2013 开发——开发自定义操作APP
博客地址:http://blog.csdn.net/FoxDave 自定义操作即我们所说的Ribbon和ECB(Edit Control Block),在SharePoint 2013之前,我们可以 ...
- SharePoint安全 - SharePoint网站常用页面URL索引
博客地址 http://blog.csdn.net/foxdave 一. 主要网站内容 首页 /default.aspx /Pages/default.aspx 网站设置 /_layouts/sett ...
- 嵌套遍历<s:iterator>map=new TreeMap(string,Map(string,User))
//嵌套遍历,先给外层的map(假设是放在root中的,如果放在context的map中,要加#)取个别名,放到Actioncontext中 <s:iterator value="ma ...
- ASP.NET是如何在IIS下工作的[转]
ASP.NET与IIS是紧密联系的,由于IIS6.0与IIS7.0的工作方式的不同,导致ASP.NET的工作原理也发生了相应的变化. IIS6(IIS7的经典模式)与IIS7的集成模式的不同 IIS6 ...
- JS 数组去重!!!
var arr = [1,2,3,1,1,1,3,5,3,6,2]; var newArr=[]; for(var i = 0; i < arr.length-1; i++){ var onOf ...