Description
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1
or 0
.
Example 1:
1 | Input: a = "11", b = "1" |
Example 2:
1 | Input: a = "1010", b = "1011" |
解法
与模拟加法实现的过程相同,只不过是二进制而已,注意不同长度的字符相加和进位的处理,同时记得处理多余的前导0。
具体代码如下:
1 | class Solution { |