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

The input strings are both non-empty and contains only characters 1or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

---------------------------------------------------------------------------------------------------------------------------------------------

这个题说的是将两个以字符串形式的二进制进行二进制加法运算后得到的结果,结果是string形式。

1)

先从最右端遍历,一个一个相加,可以用一个辅助变量来作为要进位的数,如果其中一个已经遍历完,而另一个还没遍历完,再继续遍历这个没有遍历完的字符串(并不是与另一个字符串二进制对应的数相加)。

用stringstream来将一个int类型的数转换为string类型,不过花的时间可能会比较长。

C++代码:

class Solution {
public:
string addBinary(string a, string b) {
int len1 = a.length();
int len2 = b.length();
int i = len1-,j = len2-;
int carry = ; //进位。
string str = "";
while(true){
if(i < || j < ){
break;
}
int num1 = a[i] - '';
int num2 = b[j] - '';
int sum = num1 + num2 + carry;
stringstream ss;
string s;
ss << (sum%);
ss >> s;
str = s + str;
carry = sum / ; //向前进1或进0。
i--;
j--;
}
if(j >= ){
while(j >= ){
int num = b[j] - '';
int sum = num + carry;
stringstream ss;
string s;
ss << (sum%);
ss >> s;
str = s + str;
carry = sum / ;
j--;
}
}
if(i >= ){
while(i >= ){
int num = a[i] - '';
int sum = num + carry;
stringstream ss;
string s;
ss << (sum%);
ss >> s;
str = s + str;
carry = sum / ;
i--;
}
}
if(carry > ){
stringstream ss;
string s;
ss << carry;
ss >> s;
str = s + str;
}
return str;
}
};

2)

也可以直接用char(num + '0'),将一个int类型的数转换为char类型。这样会大大提升速度。

C++代码:

class Solution {
public:
string addBinary(string a, string b) {
int len1 = a.length();
int len2 = b.length();
int i = len1-,j = len2-;
int carry = ;
string str = "";
while(true){
if(i < || j < ){
break;
}
int num1 = a[i] - '';
int num2 = b[j] - '';
int sum = num1 + num2 + carry;
str = char(sum% + '') + str;
carry = sum / ;
i--;
j--;
}
if(j >= ){
while(j >= ){
int num = b[j] - '';
int sum = num + carry;
str = char(sum% + '') + str;
carry = sum / ;
j--;
}
}
if(i >= ){
while(i >= ){
int num = a[i] - '';
int sum = num + carry;
str = char(sum % + '') + str;
carry = sum / ;
i--;
}
}
if(carry > ){
str = char(carry + '') + str;
}
return str;
}
};

3)

还可以吧上面的代码进一步优化,缩短代码行。要熟练地运用三元表达式。

C++代码:

class Solution {
public:
string addBinary(string a, string b) {
int len1 = a.length();
int len2 = b.length();
int carry = ;
string str = "";
for(int i = len1 - ,j = len2 - ;i >= || j >= ;i--,j--){
int num1 = i >= ? a[i] - '' : ;
int num2 = j >= ? b[j] - '' : ;
int sum = num1 + num2 + carry;
str = char(sum % + '') + str;
carry = sum / ;
}
if(carry > ){
str = char(carry + '') + str;
}
return str;
}
};

4)

这个char还可以用to_string()代替,用to_string(num),这个是<string> 内置的。不过运行速度会变慢。。。。

C++代码:

class Solution {
public:
string addBinary(string a, string b) {
int len1 = a.length();
int len2 = b.length();
int carry = ;
string str = "";
for(int i = len1 - ,j = len2 - ;i >= || j >= ;i--,j--){
int num1 = i >= ? a[i] - '' : ;
int num2 = j >= ? b[j] - '' : ;
int sum = num1 + num2 + carry;
str = to_string(sum % ) + str;
carry = sum / ;
}
if(carry > ){
str = to_string(carry) + str;
}
return str;
}
};

(String) leetcode 67. Add Binary的更多相关文章

  1. LeetCode 67. Add Binary (二进制相加)

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

  2. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  3. LeetCode 67. Add Binary

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

  4. Java [Leetcode 67]Add Binary

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

  5. [leetcode]67. Add Binary 二进制加法

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  6. LeetCode - 67. Add Binary(4ms)

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  7. leetcode 67. Add Binary (高精度加法)

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

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

    翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...

  9. leetCode 67.Add Binary (二进制加法) 解题思路和方法

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

随机推荐

  1. python中的zip()函数和map()函数

    一.zip()函数 1.语法: zip(iterable, ...) 参数说明: iterable,...-- 一个或多个迭代器; 在python2中: zip() 函数用于将可迭代的对象作为参数,将 ...

  2. java基础(四):谈谈java中的IO流

    1.字节流 1.1.字节输出流output 1.1.1.数据写入文件中 通过api查找output.找到很多,其中java.io.OutputStream,OutputStream: 输出字节流的超类 ...

  3. 使用Huginn抓取Discourse论坛

    Hi! I don't know why the xpath does not work, but have an easier solution. Discourse also has a JSON ...

  4. Android视频录制从不入门到入门系列教程(四)————Camera Parameter

    Camera提供了一个叫做setParameters的方法帮助开发者设置相机的相关参数. 通过Camera的getParameters方法可以获取到当前为相机设置的相关参数. 下面简单介绍下,视频录制 ...

  5. [idea] SpringBoot整合swagger2实现CRUD

    一:创建SpringBoot ,在pom.xml文件中加入jar包 <dependency> <groupId>io.springfox</groupId> < ...

  6. centos6+ 安装python3.6以及pip3

    https://blog.csdn.net/weixin_42350212/article/details/83008248

  7. redis编译 报告错误 jemalloc/jemalloc.h:没有那个文件或目录 解决.

    问题原因:没找到jemalloc头文件. 百度谷歌半天没找到有效的下载地址. github中有 到github下载.jemalloc https://github.com/jemalloc/jemal ...

  8. mange

    from flask import Flask app = Flask(__name__) manager = Manager(app) 1. 重写Command class ShellCommand ...

  9. servlet运行机制、Request内置对象和服务器端跳转

    servlet运行机制: 当发送一个请求到服务器的时候,容器(Tomcat)会判断该路径属于哪一个 Servlet 进行处理,Servlet 有一个抽象父类“HttpServlet”,这个类是一个模板 ...

  10. IDEA导入项目jar包红线、依赖问题....

    一般遇到这种情况用以下两种方式解决....... 1.reimport包 2.清缓存重启 针对1方法: a.确实不缺包: 可以先注释掉pom文件中的jar包, 此时idea会提示import, 那就i ...