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 ...
随机推荐
- 常用的正则表达式(例如:匹配中文、匹配html)(转载)
匹配中文字符的正则表达式: [u4e00-u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串 ...
- js打印数组查看
alert() 是不能查看数组,对象的console.log(数组变量); 然后你用火狐的friebug 在控制台查看
- javascript photo http://www.cnblogs.com/5ishare/tag/javascript/
- android 判断网络是否连接
package com.liucanwen.baidulocation.util; import android.app.Activity; import android.content.Contex ...
- 【转发】linux yum命令详解
linux yum命令详解 yum(全 称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理, ...
- iOS多线程之GCD学习笔记
什么是GCD 1.全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 2.纯C语言,提供了非常多强大的函数 GCD的优势 GCD是苹果公司为多核的并行运算提出的解决方案 G ...
- julia文件合并排序.jl
julia文件合并排序.jl """ julia文件合并排序.jl http://bbs.bathome.net/thread-39841-1-1.html 2016年3 ...
- jQuery 自定义扩展,与$冲突处理
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 用C#感受MongoDB MapReduce之魅力 转
MapReduce这个名词随着hadoop的用户的增多,越来越被人关注.MapReduce可谓MongoDB之中的亮点,我也想深入了解MapReduce,加上MongoDB操作简单,所以就选择了它.M ...
- c++的调试与运行
编译F9:运行F10:编译运行F11. 设置断点:在代码所在行的行首单击,该行即被加亮.注意:设置断点后,此时程序运行进入调试状态,要想运行程序,就不能使用F10或者F11,而是要使用F5调试,然后使 ...