Leetcode 67 Add Binary
来源:互联网 发布:产品效果图建模软件 编辑:程序博客网 时间:2024/06/08 10:01
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 == null || b == null){ return null; } StringBuffer sb = new StringBuffer(); int i = a.length() - 1; int j = b.length() - 1; int sum = 0; int carrybit = 0; while(i >= 0 || j >= 0){ if(i >= 0){ sum += a.charAt(i--) - '0'; } if(j >= 0){ sum += b.charAt(j--) - '0'; } sb.append(String.valueOf(sum % 2)); carrybit = sum / 2; sum = carrybit; } if (carrybit != 0) sb.append(carrybit); return sb.reverse().toString(); }}
阅读全文
0 0
- [leetcode 67] Add Binary
- [LeetCode 67]Add Binary
- leetcode-67 Add Binary
- leetcode || 67、Add Binary
- LeetCode 67 Add Binary
- Add Binary - LeetCode 67
- 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
- leetcode 67Add Binary
- Leetcode 67:Add Binary
- leetcode 67 Add Binary
- [leetcode 67] Add Binary
- spring AOP
- 【经典算法】Dijkstra单源最短路径算法
- 读《STL源码剖析》第二章 配置器 has_trivial_destructor
- 二极管的特性与分类
- EAP 资料下发与上机
- Leetcode 67 Add Binary
- nova-api到instance创建
- 六、1、对象属性的四种特性
- 自学qt之信号和槽
- paste
- java迭代器的一般用法-转
- java swing的一点心得
- python基础学习三:数据类型
- 移动字符串内容(你可以吗)