[LeetCode] Add Binary
来源:互联网 发布:淘宝宝贝排名靠前 编辑:程序博客网 时间:2024/06/06 08:30
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
For example,
a = "11"
b = "1"
Return "100".
public class Solution { public String addBinary(String a, String b) { if (a == null || a.length() < 1) { return b; } if (b == null || b.length() < 1) { return a; } int aLen = a.length(); int bLen = b.length(); int maxLen = aLen > bLen ? aLen : bLen; String res = ""; char carry = '0'; for (int i = 0; i < maxLen; i++) { int num1 = 0; if (carry == '1') { num1++; } if (i < aLen && a.charAt(aLen - i - 1) == '1') { num1++; } if (i < bLen && b.charAt(bLen - i - 1) == '1') { num1++; } switch (num1) { case 0 : carry = '0'; res = "0" + res; break; case 1 : carry = '0'; res = "1" + res; break; case 2 : carry = '1'; res = "0" + res; break; case 3 : carry = '1'; res = "1" + res; break; } } if (carry == '1') { res = "1" + res; } return res; }}
0 0
- LeetCode: Add Binary
- [Leetcode] Add Binary
- LeetCode Add Binary
- Leetcode: Add Binary
- [Leetcode] Add Binary
- LeetCode : Add Binary
- LeetCode Add Binary
- [LeetCode] Add Binary
- leetcode 58: Add Binary
- leetcode Add Binary
- [LeetCode] Add Binary
- 【leetcode】Add Binary
- [LeetCode]Add Binary
- [leetcode]Add Binary
- LeetCode-Add Binary
- [leetcode] Add Binary
- LeetCode - Add Binary
- leetcode:Add Binary
- VNC登录linux后桌面不显示图标故障解决方法
- camera 管脚功能和调试分析
- linux学习路线
- 【LeetCode笔记】Candy
- Best Time to Buy and Sell Stock II
- [LeetCode] Add Binary
- IIS6,IIS7中查看w3wp进程
- SPI中的极性CPOL和相位CPHA是什么以及如何设置
- 正则表达式---简单使用
- 在Linux中jdk环境变量的配置
- Lesson 6 Testing and Debugging
- android的service组件不被杀死
- 单片机外部中断应用
- 怎样在VS2010中打开vs2012的项目