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

对两个二进制字符串求和。

对于字符串 ans_str,求它的翻转。

reverse(ans_str.begin(), ans_str.end())

也可以:

s.assign(ans_str.rbegin(),ans_str.rend())

class Solution {
public:
string addBinary(string a, string b) {
if(a.empty())
return b;
if(b.empty())
return a; // b is greater
if(a.size() > b.size())
{
string t = a; a = b; b = t;
} string ans;
size_t i_a = a.size();
size_t i_b = b.size();
int carry = ;
int sum = ;
while(i_a-- && i_b--)
{
sum = a[i_a] - '' + b[i_b] - '' + carry;
carry = sum >= ? : ;
ans.push_back(sum % + '');
} while(i_b--)
{
sum = b[i_b] - '' + carry;
carry = sum >= ? : ;
ans.push_back(sum % + '');
} if(carry)
ans.push_back(''); reverse(ans.begin(),ans.end()); return ans;
}
};

LeetCode OJ-- Add Binary的更多相关文章

  1. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  2. LeetCode 面试:Add Binary

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

  3. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  4. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  5. 【LeetCode OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

  6. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  7. 【LEETCODE OJ】Binary Tree Preorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...

  8. LeetCode OJ 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode OJ——Validate Binary Search Tree

    http://oj.leetcode.com/problems/validate-binary-search-tree/ 判断一棵树是否为二叉搜索树.key 是,在左子树往下搜索的时候,要判断是不是子 ...

  10. LeetCode OJ 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. [译]The Python Tutorial#1. Whetting Your Appetite

    [译]The Python Tutorial#Whetting Your Appetite 1. Whetting Your Appetite 如果你需要使用计算机做很多工作,最终会发现很多任务需要自 ...

  2. Spring.Net初认识——竹子整理

    留个脚印,过两天总结. 看到知乎上有人对于DI|IOC 的解释,满不错,收藏下先 作者:OneNoodle链接:http://www.zhihu.com/question/23277575/answe ...

  3. python实现分布式进程

    今天用python实现分布式,基于python2.7,注意:在linux下执行测试通过,在windows测试失败.# -*- coding: utf-8 -*-__author__ = 'dell'i ...

  4. Android四大组件之服务

    创建一个服务,并与活动绑定 作为安卓四大组件之一的服务,毫无例外也要在manifast中进行注册 新建服务类继承于Service,并覆盖onBind( )方法,用于与活动绑定 public class ...

  5. android/libs/libammsdk.jar" already exists! 解决方法

    Error: Uh oh!"/work/2016/fengkongbao/.meteor/local/cordova-build/platforms/android/libs/libamms ...

  6. BZOJ 4393: [Usaco2015 Dec]Fruit Feast

    DP #include<cstdio> using namespace std; int T,A,B,F[5000005],G[5000005]; int main(){ scanf(&q ...

  7. DevOps实施的三种IT障碍

    [TechTarget中国原创] 现今DevOps可谓是红遍半边天,但正因为它是新的东西,企业也在不停的犯同样的错误.从这些挑战中学习,让你的DevOps项目取得成功. DevOps正在以一种更有效的 ...

  8. 我给女朋讲编程网络系列(2)--IIS8 如何在本地发布网站

    通过IIS8 在本地发布网站,一个截图,你就全明白了,越是简单,越是实用. 如果有现成的网站,就将你的网站放到一个文件夹中,比如WebTest2中. 如何没有网站,可以在WebTest2中新建一个in ...

  9. CentOS 7使用dnf安装Memcached以及启动、停止、开机启动等设置

    1.安装Memcached dnf install memcached 根据提示完成安装 2.启动Memcached 输入以下命令: service memcached start 输出以下内容: R ...

  10. 在windows64位上安装Python3.0

    1.下载安装包 下载地址:https://www.python.org/downloads/ 如果要下载帮助文件:Download Windows help file 如果要下载基于网页的安装程序: ...