LeetCode 67 Add Binary(二进制相加)(*)

简介: 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50623435 翻译给定两个二进制字符串,返回它们的和(也是二进制字符串)。
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50623435

翻译

给定两个二进制字符串,返回它们的和(也是二进制字符串)。

例如,
a = "11"
b = "1"
返回 "100".

原文

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

分析

我一开始写了这个算法,虽然实现了功能,不过不符合题目的用意。

int ctoi(char c) {
    return (int)c - 48;
}

int pow2(int n) {
    int sum = 1;
    while ((n--) > 0)
        sum *= 2;
    return sum;
}

int btoi(string s) {
    int sum = 0, len = s.size();
    for (int i = 0; i < len; ++i) {
        sum += ctoi(s[i]) * pow2(len - i - 1);
    }
    return sum;
}

string itob(int n) {
    if (n == 0) return "0";
    string s = "";
    while (n >= 1) {
        if (n % 2 == 1)
            s += "1";
        else s += "0";
        n /= 2;
    }
    string newStr = "";
    for (int i = s.size() - 1; i >= 0; i--)
        newStr += s[i];
    return newStr;
}

string addBinary(string a, string b) {
    return itob(btoi(a) + btoi(b));
}

然后改了改,写出这么脑残的代码我自己都醉了……

string addBinary(string a, string b) {
    if (a.size() < b.size()) swap(a, b);
    int len1 = a.size(), len2 = b.size();
    int comLen = len1 < len2 ? len1 : len2;
    int pos = 0;
    string str = "";
    for (int index1 = len1 - 1, index2 = len2 - 1; index1 > len1 - comLen, index2 >= 0; index1--, index2--) {
        if (pos == 0) {
            if (a[index1] == '1' && b[index2] == '1') {
                str += "0";
                pos = 1;
            }
            else if (a[index1] == '0' && b[index2] == '0') {
                str += "0";
            }
            else {
                str += "1";
            }
        }
        else if (pos == 1) {
            if (a[index1] == '1' &&b[index2] == '1') {
                str += "1";
                pos = 1;
            }
            else if (a[index1] == '0' && b[index2] == '0') {
                str += "1";
                pos = 0;
            }
            else {
                str += "0";
                pos = 1;
            }
        }
    }

    for (int index = len1 - comLen-1; index >= 0; index--) {
        if (pos == 0) {
            if (a[index] == '1') {
                str += "1";
            }
            else {
                str += "0";
            }
        }
        else if (pos == 1) {
            if (a[index] == '1') {
                str += "0";
                pos = 1;
            }
            else {
                str += "1";
                pos = 0;
            }
        }
    }
    if (pos == 1) str += "1";
    string newStr = "";
    for (int i = str.size() - 1; i >= 0; i--)
        newStr += str[i];
    return newStr;
}

转了一圈也没发现非常简洁的代码,那就先这样了……

目录
相关文章
|
5月前
|
Java 编译器
LeetCode 190. 颠倒二进制位
LeetCode 190. 颠倒二进制位
21 0
【LeetCode-每日一题】-67. 二进制求和
【LeetCode-每日一题】-67. 二进制求和
|
6月前
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
24 0
|
6月前
Leetcode Binary Tree Postorder Traversal(面试题推荐)
非递后续归遍历二叉树,肯定得用到栈。先序遍历很好写,但后续遍历就不是那么容易了。 只需要设置个指针pre,指向最后输出的那个节点就行了,只要判断cur指针指向的是上次输出节点的父节点,且cur无其他未遍历的节点,这个时候就把cur节点输出即可,然后更改pre。原理是要遍历当前节点,其所有子节点都必须遍历完,因为肯定是先左后右,所以只需一个指针保持前一次输出的结果即可。
22 0
|
6月前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
16 0
|
2月前
LeetCode[题解] 2864. 最大二进制奇数
LeetCode[题解] 2864. 最大二进制奇数
11 0
|
7月前
【Leetcode -748.最短补全词 -762.二进制表示中质数个计算置位】
【Leetcode -748.最短补全词 -762.二进制表示中质数个计算置位】
22 0
|
4月前
leetcode:190. 颠倒二进制位
leetcode:190. 颠倒二进制位
11 0
|
4月前
leetcode-1784:检查二进制字符串字段
leetcode-1784:检查二进制字符串字段
16 0
|
4月前
leetcode-67:二进制求和
leetcode-67:二进制求和
22 0

热门文章

最新文章