Leetcode: 67. Add Binary

二进制加法 https://discuss.leetcode.com/topic/33693/another-simple-java
public String addBinary(String a, String b) {
if(a==null||b==null){
return a==null?b:a; //如果其中一个为null,则返回另一个;结合着if条件语句可以包括两个都是null的情形
}
int alen = a.length()-1;
int blen = b.length()-1;
StringBuffer sb = new StringBuffer();
int carry=0;
for(;alen>=0||blen>=0||carry>0;alen--,blen--){ //终止条件:指针a,指针b都遍历完了,并且carry为0
int sum=0; //这里的求和是对字符加减,所以采用了当前字符减去‘0‘字符就可以得到数值
sum+= (alen>=0)?a.charAt(alen)-'0':0; //先加上a的值
sum+=(blen>=0)?b.charAt(blen)-'0':0; //再加上b的值
sum+=carry;
carry = sum/2;
sum = sum%2;
sb.append(sum);
}
// if(carry>0)
// sb.append(carry);
return sb.reverse().toString();
}
Leetcode: 67. Add Binary的更多相关文章
- 【LeetCode】67. Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...
- 【LeetCode】67. Add Binary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...
- 【一天一道LeetCode】#67. Add Binary
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
- 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"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
随机推荐
- 第五篇、css补充二
一.内容概要 1.图标 2.目录规划 3.a标签中的img标签在浏览器中的适应性 4.后台管理系统设置 5.边缘提示框 6.登录页面图标 7.静态对话框 8.加减框 补充知识: line-height ...
- debian下配置dynamic printk以及重新编译内核
在以前的一篇博文<编译debian内核>已经提过了重新编译内核的方法,但是整个过程花费时间较长,并且生成deb包. 这里我采用稍微简单一些的方法,因为我并没有对内核或者驱动代码做任何修改, ...
- POJ 之2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20003 Accepted: 10063 D ...
- Docker-使用Dockerfile创建镜像
Dockerfile是一个文本格式的配置文件,用户可以使用Docker来快速创建自定义的镜像 基本结构 Dockerfile由一行行命令语句组成,并且支持以#开头的注释行 一般而言,Dockerfil ...
- asp.net ajax实现md5加密
1. [图片] asp.net ajax 效果截图.png 2. [代码]前端代码HTML/Javascript/jQuery <!DOCTYPE html PUBLIC "-//W3 ...
- Codeforces 478D Red-Green Towers:dp
题目链接:http://codeforces.com/problemset/problem/478/D 题意: 给你r个红方块和g个绿方块,让你用这些方块堆一个塔. 最高层有1个方块,每往下一层块数+ ...
- 分享知识-快乐自己:Java常用API总结
1):java.io.BufferedReader类(用于从文件中读入一段字符:所属套件:java.io) 1. 构造函数BufferedReader(java.io.FileReader FileR ...
- poj-1379 Run Away(模拟退火算法)
题目链接: Run Away Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7982 Accepted: 2391 De ...
- leetcode 201. Bitwise AND of Numbers Range(位运算,dp)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- MarkDown不支持图片放缩。。
![](http://images2015.cnblogs.com/blog/573995/201604/573995-20160430162211050-1357272526.jpg =100x20 ...