67. Add Binary
来源:互联网 发布:淘宝whoo小样是真的吗 编辑:程序博客网 时间:2024/06/05 19:47
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
public class Solution { public String addBinary(String a, String b) { if(a.length() < b.length()){ String tmp = a; a = b; b = tmp; } int pa = a.length()-1; int pb = b.length()-1; int carries = 0; String rst = ""; while(pb >= 0){ int sum = (int)(a.charAt(pa) - '0') + (int)(b.charAt(pb) - '0') + carries; rst = String.valueOf(sum % 2) + rst; carries = sum / 2; pa --; pb --; } while(pa >= 0){ int sum = (int)(a.charAt(pa) - '0') + carries; rst = String.valueOf(sum % 2) + rst; carries = sum / 2; pa --; } if (carries == 1) rst = "1" + rst; return rst; }}
0 0
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 67. Add Binary
- 【leetcode】67. Add Binary
- 67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 67. Add Binary
- 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary
- 67. Add Binary LeetCode
- 67. Add Binary
- 【LeetCode】67. Add Binary
- 67. Add Binary
- 67. Add Binary
- 深入Java关键字null
- 社团划分——有向图的Label Propagation算法
- 执行mvn 报错 source-7或source 1.5 中不支持 diamond运算符
- HDU 5701 中位数计数 (百度之星初赛)
- androidstudio中将library生成为arr文件
- 67. Add Binary
- Java中的String
- 欢迎使用CSDN-markdown编辑器
- C/C++头文件一览
- 【poj 2976】 Dropping tests 二分(分数优化)
- 70. Climbing Stairs
- eclipse 断点进不去 显示 source not found
- LambdaMART的思想
- [PKU 3046] Ant Counting (计数DP+差分加数)