(java)leetcode-67:Add Binary
2017-07-23 11:34
399 查看
Add Binary
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) {
String result = "";
int alen = a.length()-1;
int blen = b.length()-1;
int addnum = 0;
while(alen>=0 || blen>=0)
{
int sum = 0;
if(alen >= 0)
sum += a.charAt(alen) - 48;
if(blen >= 0)
sum += b.charAt(blen) - 48;
int c = sum+addnum;
sum = c%2;
addnum = c/2;
result = "" + sum + result;
alen--;
blen--;
}
if(addnum!= 0)
result = "" + addnum + result;
return result;
}
}
相关文章推荐
- LeetCode 67 — Add Binary(C++ Java Python)
- Leetcode:67. Add Binary(JAVA)
- 【leetcode】67. Add Binary【java】
- Java [Leetcode 67]Add Binary
- leetcode_67 add binary
- [Leetcode] Add Binary (Java)
- Leetcode 67 Add Binary
- LeetCode67 Add Binary 解题报告
- LeetCode(67) Add Binary
- [LeetCode] 67. Add Binary 二进制数相加
- leetcode 67. Add Binary
- [Leetcode 67, easy] Add Binary
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
- [leetcode] 67.Add Binary
- leetcode_question_67 Add Binary
- [leetcode-67]Add Binary(C)
- Add Binary - LeetCode 67
- leetcode 67---Add Binary 二进制 进位 String
- leetcode 67 Add Binary
- LeetCode(67) Add Binary