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) {
int len1=a.size();
int len2=b.size();
int c=;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string s="";
int len=max(len1,len2);
for(int i=;i<len;i++)
{
int t=;
if(i<len1) t+=a[i]-'';
if(i<len2) t+=b[i]-'';
t+=c;
c=t/;
s=(char)(t%+'')+s;
}
if(c>) s=(char)(c+'')+s;
return s;
}
};

Add Binary <leetcode>的更多相关文章

  1. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

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

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  3. Add Binary

    Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...

  4. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  5. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. 2016.6.21——Add Binary

    Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...

  8. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  9. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

  10. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

随机推荐

  1. 解决CentOS6.4 Docker "Couldn't connect to Docker daemon ..." 问题

    OS: CentOS6.4 #uname -r 2.6.32-504.1.3.el6.x86_64 安装完毕fig,并完成相应配置时执行如下命令出错(fig安装参见:http://www.fig.sh ...

  2. js创建对象的6种方式

    一.工厂模式 function createStudent(name,age){ var o=new Object(); o.name=name; o.age=age; o.myName=functi ...

  3. mysql 可能会用到的一些 函数

    DATE_SUB(sysdate(), INTERVAL 8 HOUR)  //当前时间前 8小时 ifnull("","")  Parameter //添加一 ...

  4. Couldn't open file on client side, trying server side 错误解决

    09-09 09:43:21.651: D/MediaPlayer(3340): Couldn't open file on client side, trying server side09-09 ...

  5. POJ 2481 Cows(树状数组)

                                                                      Cows Time Limit: 3000MS   Memory L ...

  6. Linux——常用命令详解

    文件处理命令:ls  -l drwxr-xr-x   文件 d   rwx  r-x   r-x d:表示directory  是一个目录         - 表示二进制文件    l 表示链接文件l ...

  7. VISIBLE、INVISIBLE、GONE的区别

    VISIBLE:设置控件可见 INVISIBLE:设置控件不可见 GONE:设置控件隐藏 而INVISIBLE和GONE的主要区别是:当控件visibility属性为INVISIBLE时,界面保留了v ...

  8. asp.net ajax 调用一例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  9. How to Allow MySQL Client to Connect to Remote MySql

    How to Allow MySQL Client to Connect to Remote MySQ By default, MySQL does not allow remote clients ...

  10. [Linux] CentOS 加入开机启动

    1.在/etc/init.d/目录下新建一个文件:autostart.sh #!/bin/sh #chkconfig: 2345 80 80 #description: auto start web ...