67. 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) {
        StringBuilder sb = new StringBuilder();
		int dif = a.length() - b.length(), C = 0, curRes, curA, curB;
		if (dif > 0) {
			String temp = a;
			a = b;
			b = temp;
		} else
			dif = -dif;
		sb.append(a);
		while (dif-- > 0)
			sb.insert(0, '0');
		a = sb.toString();
		sb.delete(0, sb.length());
		for (int i = a.length() - 1; i >= 0; i--) {
			curA = a.charAt(i) - '0';
			curB = b.charAt(i) - '0';
			curRes = curA ^ curB ^ C;
			C = (curA ^ curB) & C | curA & curB;
			sb.insert(0, (char) (curRes + '0'));
		}
		if (C == 1)
			sb.insert(0, '1');
		return sb.toString();
    }
}


阅读更多
版权声明:本文为博主原创文章,转载请声明原文出处:http://blog.csdn.net/xiangwanpeng https://blog.csdn.net/xiangwanpeng/article/details/52950486
文章标签: leetcode 算法
个人分类: leetcode
所属专栏: LeetCode Solutions In Java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭