67. Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
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
;
}
};
本文探讨了如何实现两个二进制字符串的相加,并提供两种不同的C++实现方案。一种是作者自己的实现思路,另一种来自他人的高效实现。通过对比,文章强调了合理运用位操作、提前分配内存空间等技巧的重要性。
314

被折叠的 条评论
为什么被折叠?



