Add BinaryApr 2 '12 3558 / 10570

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的更多相关文章

  1. [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 ...

  2. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  3. 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 ...

  4. [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 ...

  5. 【LEETCODE OJ】Binary Tree Postorder Traversal

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

  6. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  7. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  9. (二叉树 递归) 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 ...

  10. (二叉树 递归) 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 ...

随机推荐

  1. POJ 1113 Wall 求凸包的两种方法

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31199   Accepted: 10521 Descriptio ...

  2. asp.net 从客户端中检测到有潜在危险的 Request.Form 值错误解

    从客户端(ftbContent="<P><A href="http://l...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验 ...

  3. Oracle解析 xml 记录一下(未完待续)

    Oracle解析 xml 记录一下. SQL> desc xmlparser; PROCEDURE FREEPARSER Argument Name                  Type  ...

  4. Oracle_Flashback_技术_总结

    Oracle Flashback 技术 总结 Flashback 技术是以Undo segment中的内容为基础的, 因此受限于UNDO_RETENTON参数.要使用flashback 的特性,必须启 ...

  5. 无法升级数据库....因为此版本的 SQL Server 不支持该数据库的非发布版本(539) 解决方案

    使用SQL2012附加一个数据库时报出了以下错误:“无法升级数据库....因为此版本的 SQL Server 不支持该数据库的非发布版本(539).不能打开与此版本的 sqlserver.exe 不兼 ...

  6. 你好,C++(32) 类是对现实世界的抽象和描述 6.2.1 类的声明和定义

    6.2  类:当C++爱上面向对象 类这个概念是面向对象思想在C++中的具体体现:它既是封装的结果,同时也是继承和多态的载体.因此,要想学习C++中的面向对象程序设计,也就必须从“类”开始. 6.2. ...

  7. terminal命令

    新建一个文件并写入内容:  echo hello world > a.txt (每次echo都会重写文件) 新建文件: touch a.txt 新建目录: mkdir work 用vim打开文件 ...

  8. vector list array deque

    因此在实际使用时,如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面 的原则:   1.如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector   2.如果你需要大量的插入和 ...

  9. shell之小括号、中括号、大括号

    1.Shell中变量的原形:${var}  一串命令的执行 #等价于 $ var=test $ echo $var test #例如,用在这个位置 $ echo ${var}AA testAA 2.命 ...

  10. laravel框架——上传、下载文件

    文件上传 在config文件夹下新建一个 项目名.php return [ 'title' => 'My Test', 'posts_per_page' => 5, 'uploads' = ...