https://leetcode.com/problems/add-binary/

题目:

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) {
string c="",d="";
if(a==""&&b=="")
return "";
int a_t=a.size(),b_t=b.size(),min_t=min(a_t,b_t),max_t=max(a_t,b_t);
int tmp_a,tmp_b,jinyi=;
for(int i=;i<max_t;i++){
if(i<a_t)
tmp_a=a[a_t-i-]-'';
else
tmp_a=;
if(i<b_t)
tmp_b=b[b_t-i-]-'';
else
tmp_b=;
c.push_back((tmp_a+tmp_b+jinyi)%+'');
if(tmp_a+tmp_b+jinyi>=)
jinyi=;
else
jinyi=;
}
if(jinyi==)
c.push_back('');
for(int i=c.size()-;i>=;i--)
d.push_back(c[i]);
return d;
}
};

LeetCode(67)题解: Add Binary的更多相关文章

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

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

  2. leetcode解题:Add binary问题

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

  3. leetcode笔记:Add Binary

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

  4. LeetCode(67) Add Binary

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

  5. LeetCode算法题-Add Binary(Java实现)

    这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...

  6. LeetCode OJ:Add Binary(二进制相加)

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

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

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

  8. 67. Add Binary【LeetCode】

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

  9. LeetCode练题——67. Add Binary

    1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...

随机推荐

  1. EIGRP

    因为rip的收敛时间长  尤其是使用过程中   链路down掉    重收敛的时间比较长  所以在中到大型的园区网中很少用到rip协议 只有在很小的局域网中用到rip   因为收敛时间可能会稍微短一些 ...

  2. enable apache2 rewrite_mod on ubuntu14.04

    $ cd /etc/apache2/mods-enable $ ln -s rewrite.load ../mods-available/rewrite.load change "Allow ...

  3. 菜鸡的2017CPPC网络赛

    Friend-Graph Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. ER模型图工具:PowerDesigner

    导读:开始是想一个阶段一个阶段的写文档,再写了可行性分析报告之后,发现这种方法整体性不强,写出来的文档也总是有考虑不周的地方.后来,就看完了全部的软工视频再写.现在该写项目开发总结报告了.在写文档的过 ...

  5. spring运行时没有问题,在单元测试时,出现java.lang.ClassFormatError错误

    Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstrac ...

  6. BZOJ 4004 [JLOI2015]装备购买 ——线性基

    [题目分析] 题目很简单,就是要维护一个实数域上的线性基. 仿照异或空间的线性基的方法,排序之后每次加入一个数即可. 卡精度,开long double 和 1e-6就轻松水过了. [代码] #incl ...

  7. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  8. 【HDOJ5952】Counting Cliques(团,dfs)

    题意:给定一张n点m边的图,求大小为S的团的个数 N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10,保证点的度不超过20 思路:dfs 因为每个点可能不止属于一个极大团,所以不能求出极大团然后计 ...

  9. 【SPOJ1825】Free tour II (点分治,启发式)

    题意: 边权可能为负 思路: 感觉我自己写的还是太过僵硬了,可以灵活一点,比如可以多写几个不同的dfs求出不同的信息,而不是压到同一个dfs里 #include<cstdio> #incl ...

  10. 金鹰dreamweaver视频教程下载地址

    原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] http://download2.gbaopan.com/c188aecb5e524ab2b2a484c061e0 ...