[leetcode]67. Add Binary@Java
来源:互联网 发布:js appendchild的用法 编辑:程序博客网 时间:2024/06/05 20:21
https://leetcode.com/problems/add-binary/#/description
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
package go.jacob.day722;public class Demo2 {/* * 用StringBuilder比用String和StringBuffer速度更快 */public String addBinary(String a, String b) {StringBuilder res = new StringBuilder();int i = a.length() - 1, j = b.length() - 1, carry = 0;while (i >= 0 || j >= 0 || carry != 0) {int sum = carry;if (i >= 0)sum += a.charAt(i--) - '0';if (j >= 0)sum += b.charAt(j--) - '0';res.append(sum % 2);carry = sum / 2;}//res是倒序,必须进行反转return res.reverse().toString();}}
阅读全文
0 0
- (Java)LeetCode-67. Add Binary
- 【leetcode】67. Add Binary【java】
- [LeetCode] 67. Add Binary java
- [leetcode]67. Add Binary@Java
- [Leetcode] Add Binary (Java)
- Add Binary @ LeetCode java
- [LeetCode][Java] Add Binary
- leetcode:Add Binary 【Java】
- 【leetcode】Add Binary-----Java
- Leetcode:67. Add Binary(JAVA)
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 【leetcode】67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 【Leetcode】67. Add Binary
- 1024. 科学计数法 (20)
- Java集合源码学习(四)HashMap分析
- 2016TID敏捷持续集成演讲材料——公开版
- C语言细节总结
- 冒泡排序(C实现)
- [leetcode]67. Add Binary@Java
- Linux配置虚礼机的网络和yum源
- Oracle 索引
- java中equals(),==与hashcode()的区别?
- Netty4启动ServerBootStrap源码分析
- 类字面常量.class
- 一个整数二进制位中1的个数
- java并发编程实战-性能与可伸缩性2
- `移动端布局`、`rem单位使用`、`flexible适配`、`gulp中的sass插件和同步多屏浏览的bowsersync的使用