LeetCode 067. Add Binary

67. Add Binary

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

For example,
a = 
"11"
b = 
"1"
Return  "100" .
class Solution {
public:
    string addBinary(string a, string b) {
    }
};
解题思路:
  • 自己的解题思路
主要考虑可能出现的边界情况,如 ”0010”,”  10”; 这样的边界情况。但是,我还没有处理出现 ”   a1030  ”; 这样的情况。这样的情况应该是非法输入,但是我的程序会认为它是 ”10” 。解题的核心思路很简单,( 1 )同时存在的情况;( 2 )只有一个存在的情况,长度较长的那个;( 3 )存在最后的进位的情况。
  • 别人的解题思路
将上面的 3 种情况进行统一处理;真是厉害。而且使用了很多技巧。
学习收获:
  • 如果每次都要进行头插入的话。可以换个角度考虑问题。
先尾插入,最后再进行倒序。
附件:程序
1 、自己的程序:
class Solution
{
     public :
     string addBinary ( string a , string b )
     {
         string a1 ;
         auto i = a . find_first_of ( "01" );
         if ( i != string :: npos )
         {
             auto j = a . find_first_not_of ( "01" , i + 1 );
             if ( j == string :: npos )
             {
                 j = a . size ();
             }
             a1 . insert ( a1 . end (), a . begin () + i , a . begin () + j );
         }
         string b1 ;
         auto i1 = b . find_first_of ( "01" );
         if ( i1 != string :: npos )
         {
             auto j1 = b . find_first_not_of ( "01" , i1 + 1 );
             if ( j1 == string :: npos )
             {
                 j1 = b . size ();
             }
             b1 . insert ( b1 . end (), b . begin () + i1 , b . begin () + j1 );
         }
         //空字符
         if ( a1 . size () == 0 && b1 . size () == 0 ) { return {}; }
         if ( a1 . size () == 0 || a1 . find ( '1' ) == string :: npos )
         {
             auto tem = b1 . find ( '1' );
             if ( tem == string :: npos )
             {
                 return { '0' };
             }
             return string ( b1 . begin () + tem , b1 . end ());
         }
         if ( b1 . size () == 0 || b1 . find ( '1' ) == string :: npos )
         {
             auto tem = a1 . find ( '1' );
             if ( tem == string :: npos )
             {
                 return { '0' };
             }
             return string ( a1 . begin () + tem , a1 . end ());
         }
         string res ;
         int len1 = a1 . size ();
         int len2 = b1 . size ();
         int len3 = len1 > len2 ? len1 : len2 ;
         res . reserve ( len3 );
         int carry = 0 ;
         -- len1 ;
         -- len2 ;
         while ( len1 >= 0 && len2 >= 0 )
         {
             int sum = a1 [ len1 ] - '0' + b1 [ len2 ] - '0' + carry ;
             if ( sum > 1 )
             {
                 res . push_back (( sum % 2 ) + '0' );
                 carry = 1 ;
             }
             else
             {
                 res . push_back ( sum + '0' );
                 carry = 0 ;
             }
             -- len1 ;
             -- len2 ;
         }
         while ( len1 >= 0 )
         {
             int sum = a1 [ len1 ] - '0' + carry ;
             if ( sum > 1 )
             {
                 res . push_back (( sum % 2 ) + '0' );
                 carry = 1 ;
             }
             else
             {
                 res . push_back ( sum + '0' );
                 carry = 0 ;
             }
             -- len1 ;
         }
         while ( len2 >= 0 )
         {
             int sum = b1 [ len2 ] - '0' + carry ;
             if ( sum > 1 )
             {
                 res . push_back (( sum % 2 ) + '0' );
                 carry = 1 ;
             }
             else
             {
                 res . push_back ( sum + '0' );
                 carry = 0 ;
             }
             -- len2 ;
         }
         if ( carry ) { res . push_back ( '1' ); }
         auto lt = res . rfind ( '1' );
         auto zeros = res . size () - lt - 1 ;
         return string ( res . rbegin () + zeros , res . rend ());
     }
};
2 、别人的程序

这里有许多技巧。位操作;提前分配空间,方便插入;3操作合一;头插变尾插。

class Solution
{
     public :
     string addBinary ( string a , string b )
     {
         string s ;
         s . reserve ( a . size () + b . size ());
         int c = 0 , i = a . size () - 1 , j = b . size () - 1 ;
         while ( i >= 0 || j >= 0 || c == 1 )
         {
             c += i >= 0 ? a [ i --] - '0' : 0 ;
             c += j >= 0 ? b [ j --] - '0' : 0 ;
            
//s = char(c % 2 + '0') + s;     //this can run, but time complex is great
s . push_back (( c & 1 ) + '0' );
             c >>= 1 ;
         }
         reverse ( s . begin (), s . end ());
         return s ;
     }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值