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 ...
随机推荐
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- 剑指offer之 数值的整数次方
问题描述:实现函数double power(double base,int exponent),求base的exponent次方.不能使用库函数,同时不需要考虑大数问题. package Proble ...
- JavaScript 使用技巧(持续更新)
JavaScript 使用技巧(持续更新) 类型检测 使用Object.prototype.toString.call(obj)的方式. 因为无论typeof还是instanceof都无法做到精确判断 ...
- codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)
题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...
- json 文件解析与应用
第一步:首先弄一个 json 文件 我这里成为 config.json 内容如下 { ": { , "desc":"中华人民共和国" }, &qu ...
- 跨平台的WebRTC客户端框架:OpenWebRTC
Webrtc的ios框架编译 http://www.th7.cn/Program/IOS/201502/390418.shtml WebRTC in WebKit : http://www.webrt ...
- loj516dp一般看规律
STL 这...我只能说是...考得是... STL的正确用法? #include<iostream> #include<cstdio> #include<cstdlib ...
- CodeForces - 1017 C. The Phone Number(数学)
Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The ...
- 本机不装Oracle,使用plsql连接远程Oracle的方法
由于Oracle的庞大,有时候我们需要在只安装Oracle客户端如plsql.toad等的情况下去连接远程数据库,可是没有安装Oracle就没有一切的配置文件去支持.最后终于发现一个很有效的方法,Or ...
- APIO2017 游记
参加了APIO的同学肯定知道我为什么只写标题不写内容. QAQ