LeetCode 67. Add Binary

原创 2016年06月01日 20:32:14
问题描述:

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

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

题目大意:用字符串实现二进制数的加法。

1.字符串长度不一定同,因此反转字符串在做加法,最后得到的结果在反转。

2.注意最高位的进位。

string addBinary(string a, string b)
{
    if(a == "0")
        return b;
    if(b == "0")
        return a;
    int l1 = a.length();
    int l2 = b.length();
    //字符反转,便于相加
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());
    //将字串转换为整型数组
    int *n1 = new int[l1];
    int *n2 = new int[l2];
    for(int i = 0;i < l1;i++)
        n1[i] = a[i] - '0';
    for(int i = 0;i < l2;i++)
        n2[i] = b[i] - '0';
    //两个二进制数相加的长度不会超过最长的那个字符串的长度+1
    int len = max(l1,l2);
    int *ans = new int[len + 1];
    memset(ans,0,sizeof(int)*(len+1));
    //以下的类似于合并两个有序链表的步骤,注意处理进位,逢2进1
    int i = 0;
    int j = 0;
    //进位
    int carry = 0;
    while(i < l1 && j < l2)
    {
        ans[i] = n1[i] + n2[i] + carry;
        carry = ans[i] / 2;
        ans[i] = ans[i] % 2;
        i++;
        j++;
    }
    while(i < l1)
    {
        ans[i] = n1[i] + carry;
        carry = ans[i] / 2;
        ans[i] = ans[i] % 2;
        i++;
    }
    while(j < l2)
    {
        ans[j] = n2[j] + carry;
        carry = ans[j] / 2;
        ans[j] = ans[j] % 2;
        j++;
    }
    //最高位可能还有进位
    if(carry == 1)
        ans[len] = 1;
    string res = "";
    for(int k = 0;k < len + 1;k++)
        res = res + char(ans[k] + '0');
        //前面为了便于相加做了反转处理,这里再次反转,
    reverse(res.begin(),res.end());
    //最高位没有进位,为0的情况,0不能显示出来。
    if(res[0] == '0')
        return res.substr(1);
    else
        return res;
    }



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 67 — Add Binary(C++ Java Python)

题目:http://oj.leetcode.com/problems/add-binary/ Given two binary strings, return their sum (also a bi...

LeetCode(67) Add Binary

题目Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” ...
  • fly_yr
  • fly_yr
  • 2015-08-19 20:58
  • 1623

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

翻译给定两个二进制字符串,返回它们的和(也是二进制字符串)。例如, a = "11" b = "1" 返回 "100".原文Given two binary strings, return their...
  • NoMasp
  • NoMasp
  • 2016-02-02 13:49
  • 1844

LeetCode-67 Add Binary(二进制数相加)

LeetCode-67 Add Binary(二进制数相加) Given two binary strings, return their sum (also a binary string). ...

LeetCode笔记:67. Add Binary

模拟两个字符串二进制数的加法

Add Binary - LeetCode 67

题目描述:Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "...

【Leetcode】67. Add Binary

思路: (1)先求a、b长度的较大值len。 (2)在a和b中较短的字符串前面补齐0。 (3)用flag记录每一位的进位,result记录求和结果。 (4)从后往前遍历a和b,让相应位相加并加上fla...

LeetCode 67. Add Binary(二进制加)

原题网址:https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (also a ...

LeetCode-67. Add Binary

问题描述Given two binary strings, return their sum (also a binary string). For example, a = “11” b = ...

leetcode--67. Add Binary

leetcode
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)