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 ...
随机推荐
- spring-boot4
1.1.1. Starter pom 除了官方也有其他第三方提供的starter Websocket是服务端推数据到客户端.长连接. 1.1.1.Xml 配置文件 有些时候必须使用xml配置. 1.1 ...
- 【leetcode刷题笔记】Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- python 常用的字符串方法
st = ' hello Kitty 'str = 'hello {name} {age}' #print(st.format(name='fadfa'))#常用的字符串方法print(st.coun ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- Qt之界面实现技巧-- 窗体显示,绘制背景,圆角,QSS样式
转自 --> http://blog.sina.com.cn/s/blog_a6fb6cc90101dech.html 总结一下,在开发Qt的过程中的一些技巧!可遇而不可求... 一.主界面 1 ...
- HTML5坦克大战1
在JavaScript中,不要在变量为定义之前去使用,这样很难察觉并且无法运行. 颜色不对. 当我的坦克移动时,敌人坦克消失. tankGame3.html <!DOCTYPE html> ...
- php构造函数的继承方法
第一种情况:子类没有定义构造函数时,默认继承.例子: ? 1 2 3 4 5 6 7 8 9 10 11 12 <?php class A{ public $name; function _ ...
- Idea_学习_05_Intellij Idea自动添加注释的方法
二.参考资料 1. Intellij Idea自动添加注释的方法
- queryRuner如何获得bean对象,当这个bean对象中包含其他对象的时候
我们知道我们可以使用dbutil的QueryRunner下的query方法使用BeanHandler得到bean对象 但是,当我们在一个表和另一个表关联的时候,往往喜欢将另一个表的关联字段变成另一个b ...
- A唐纳德先生和假骰子(华师网络赛)
Time limit per test: 1.0 seconds Memory limit: 256 megabytes 在进行某些桌游,例如 UNO 或者麻将的时候,常常会需要随机决定从谁开始.骰子 ...