LeetCode OJ-- Add Binary
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的更多相关文章
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【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 ...
- 【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 ...
- 【LEETCODE OJ】Binary Tree Preorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...
- 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 ...
- LeetCode OJ——Validate Binary Search Tree
http://oj.leetcode.com/problems/validate-binary-search-tree/ 判断一棵树是否为二叉搜索树.key 是,在左子树往下搜索的时候,要判断是不是子 ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- Python中的set
set_lst = [ ('集合容器不可哈希',), ('集合中的元素必须可哈希',), ('集合是无序的',), ('集合自动去重',), ('增',), ('删',), ('查',), ('集合运 ...
- Elizabeth Taylor【伊丽莎白·泰勒】
Elizabeth Taylor People fell in love with Elizabeth Taylor in 1944, when she acted in the movie Nati ...
- 动态规划:HDU1059-Dividing(多重背包问题的二进制优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu5319 简单模拟
题意很好懂,大致就是三种颜色,红和蓝一起会变绿,给个终态矩阵,问从原始状态到终态最少画几笔? 按一定规则画 思路就是记红为1,蓝为2,绿为3,先遍历绿色,针对每一块绿色进行删除,每找到一块绿色,首先 ...
- install golang plugin in webstrom
https://github.com/go-lang-plugin-org/go-lang-idea-plugin/wiki/Documentation
- 【正则】对RegExp执行typeof运算的结果
对RegExp执行typeof运算的结果并不统一,在有些浏览器中返回“function”,在有些中返回“object”. 谷歌: 火狐 IE: **
- How to check if Visual Studio 2005 SP1 is installed
How to check if Visual Studio 2005 SP1 is installed Check the following registry key. HKEY_LOCAL_MAC ...
- IOS开发学习笔记008-预处理
预处理 1.宏定义 2.条件编译 3.文件包含 注意: 1.所有预处理都是以#开头,并且结尾不用分号. 2.宏名一般用大写字母,以便与变量名区别开来,但用小写也没有语法错误 3.作用域也是从定义到代码 ...
- [netty4][netty-common]FastThreadLocal及其相关类系列
FastThreadLocal 概述: ThreadLocal的一个特定变种改善,有更好的存取性能. 内部采用一个数组来代替ThreadLocal内部的hash表来存放变量.虽然这看起来是微不足道的, ...
- [oldboy-django][2深入django]django目录说明 + 路由系统
django project目录说明 project - app01 -- admin.py #django自带后台管理 -- apps.py #app01配置文件 -- models.py #编写类 ...