LeetCode-Add Two 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string ret;
bool carry=false;
if(a.size()>b.size())
{
int i=a.size()-1,j=b.size()-1;
while(j>=0){
int sum=a.c_str()[i]+b.c_str()[j]-2*'0';
if(carry){
sum++;
carry=false;
}
if(sum>=2){
sum-=2;
carry=true;
}
char c=sum+'0';
ret=c+ret;
i--;
j--;
}
while(i>=0){
int sum=a.c_str()[i]-'0';
if(carry){
sum++;
carry=false;
}
if(sum>=2){
sum-=2;
carry=true;
}
char c=sum+'0';
ret=c+ret;
i--;
}
}
else
{
int i=a.size()-1,j=b.size()-1;
while(i>=0){
int sum=a.c_str()[i]+b.c_str()[j]-2*'0';
if(carry){
sum++;
carry=false;
}
if(sum>=2){
sum-=2;
carry=true;
}
char c=sum+'0';
ret=c+ret;
i--;
j--;
}
while(j>=0){
int sum=b.c_str()[j]-'0';
if(carry){
sum++;
carry=false;
}
if(sum>=2){
sum-=2;
carry=true;
}
char c=sum+'0';
ret=c+ret;
j--;
}
}
if(carry)ret='1'+ret;
return ret;
}
};
LeetCode-Add Two Binary的更多相关文章
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- Upgrade to Python 2.7.9 on CentOS5.5
1. Install python2.7 #cd /tmp #wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz --no-ch ...
- javascript中this的解析
在JavaScript中,this 的概念比较复杂.除了在面向对象编程中,this 还是随处可用的.这篇文章介绍了this 的工作原理,它会造成什么样的问题以及this 的相关例子. 要根据this ...
- RAC检查各资源
- iOS移动端架构的那些事!(转载)
一个app的初始阶段,必然是先满足各种业务需求.然后,经过多次版本迭代之后,先前的由于急于满足需求而导致的杂乱代码则会充斥整个项目.而此时,项目有了一定的规模,有了一定数量的开发人员,那么为了达到快速 ...
- java_log_02
配置 在第一部分,我们将介绍配置 logback 的各种方法,给出了很多配置脚本例子.在第二部分,我们将介绍 Joran,它是一个通用配置框架,你可以在自己的项目里使用 Joran 一.Logback ...
- Hive学习之五 《Hive进阶—UDF操作案例》 详解
hive—UDF操作 udf的操作过程: 在HIVE会话中add 自定义函数的jar文件,然后创建function,继而使用函数. 下面就以下面课题为例: 课题:统计每个活动的PV和UV 一.Java ...
- linux网络编程常用头文件
sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构 netinet/in.h:定义数据结构sockaddr_in arpa/inet.h:提供IP地址转换函 ...
- 不熟悉的CSS样式
1.font连写: 例如:font:15px/23px "ralewayregular","Helvetica Neue",Helvetica,Arial,sa ...
- 我是怎样自学日语的(太TM励志了!)
学日语并不难,难是难在你有没有信心学好日语,有没有恒心学好日语.如果三天打鱼两天晒网的话,我劝你还是趁早死心,在语言世界里没有任何的捷径,有的只是艰辛和不懈的努力. 我自认为自己在学语言方面很有天赋, ...
- Razor引擎的转换数据类型
AsInt() 把字符串转换为整数. if (myString.IsInt()) IsInt() {myInt=myString.AsInt();} AsFloat() 把字符串转换为浮点数. if ...