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. C# 各种数据类型的最大值和最小值常数

    using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO ...

  2. codeforces195c

    link:http://codeforces.com/problemset/problem/336/C 从大到小枚举,如果对应的二进制位不等于0,就加进来,最后的sum如果%2^k==0那么就是合法的 ...

  3. ubuntu 编译安装 srilm

    Ubuntu 64bit系统下SRILM的配置 依赖软件包(先进行): 1.c/c++ compiler:编译器gcc 3.4.3及以上版本,我的是gcc 4.4 2.GNU make:构建和管理工程 ...

  4. Spring MVC小结

    Spring MVC项目搭建 添加依赖 (省略) Spring MVC配置类 @Configuration @EnableWebMvc @ComponentScan("com.sjx.spr ...

  5. 利用Manacher算法寻找字符串中的最长回文序列(palindrome)

    寻找字符串中的最长回文序列和所有回文序列(正向和反向一样的序列,如aba,abba等)算是挺早以前提出的算法问题了,最近再刷Leetcode算法题的时候遇到了一个(题目),所以就顺便写下. 如果用正反 ...

  6. java连接mysql :No Suitable Driver Found For Jdbc 解决方法

    今天出现编码出现了No suitable driver found for jdbc,又是找遍了网上的资料,基本上都说是以下个问题:    一是:连接URL格式出现了问题(Connection con ...

  7. NBU恢复报:ORA-19554 ORA-27211

    RMAN> run {2> allocate channel ch00 type 'sbt_tape' parms='ENV=(NB_ORA_CLIENT=rac1)';3> res ...

  8. sql server中的 SET NOCOUNT ON 的含义

    每次我们在使用查询分析器调试SQL语句的时候,通常会看到一些信息,提醒我们当前有多少个行受到了影响,这是些什么信息?在我们调用的时候这些信息有用吗?是否可以关闭呢? 答案是这些信息在我们的客户端的应用 ...

  9. ios系统的中arm指令集

    arm结构处理器,几乎所有的手机都基于arm,其在嵌入式系统中应用非常广泛. ARM 处理器因为低功耗和小尺寸而闻名,它的性能在同等功耗的产品中也很出色.这里我们注意一点,模拟器并不运行arm代码,软 ...

  10. impala简单使用

    impala-shell connect ha1:21000 更新元信息 invalidate metadata;