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. dfs 的全排列

    #include <iostream> #include <algorithm> #include <cstdio> #include <string> ...

  2. datagrid的formatter

    1.formatter函数 formatter:function(value,rowData,rowIndex){ return 'xxx'; } 注意: (1)formatter一定要有返回,且返回 ...

  3. Azure Active Directory中的特权身份管理如何运作?

    [TechTarget中国原创] 用户权限不是平等的.有些用户需要有大量权利和特权——通常这些都是管理员.企业在允许特权用户进行管理以及支持活动时,还需要意识到特权用户也有可能犯错.他们会犯错.他们可 ...

  4. NOS直传加速服务

    本文来自网易云社区 作者:孙建良 最近团队在对存储系统做一些性能测试,期间遇到了不少问题,测试过程中得出的结果也没有很好的数据支撑,所以尝试了非常多的方法来对性能问题进行定位. 小王童鞋是挺厉害的,使 ...

  5. 【Best Time to Buy and Sell Stock III 】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  6. MongoDB快速入门学习笔记5 MongoDB的文档修改操作

    db.集合名称.update({query},{update},upsert, multi})query:过滤条件update:修改内容upsert:如果不存在查询条件查出的记录,是否插入一条数据,默 ...

  7. MongoDB快速入门学习笔记1 windows安装MongoDB

    1.安装MongoDB 从MongoDB官网上下载MongoDB,我下载的版本是64位的3.2.6.下载完以后直接安装,我的安装目录是D:\work\MongoDB. 2.配置MongoDB的环境变量 ...

  8. lucene.NET详细使用与优化详解

    lucene.NET详细使用与优化详解 http://www.cnblogs.com/qq4004229/archive/2010/05/21/1741025.html http://www.shan ...

  9. How Ironic Inspector Works

    翻译官网概述. 操作员将节点注册为Ironic,例如 通过openstack baremetal CLI命令. 电源管理认证应该在这一步提供给Ironic. 如节点状态所述,节点被置于正确的自省状态. ...

  10. 三种框架对比react vue 和Angular对比

    https://blog.csdn.net/runOnWay/article/details/80103880 angular 优点 背靠谷歌 使用typescript 便于后端人员开发上手 完整 不 ...