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. iOS网络HTTP、TCP、UDP、Socket 知识总结

    OSI 七层模型 我们一般使用的网络数据传输由下而上共有七层,分别为物理层.数据链路层.网络层.传输层.会话层.表示层.应用层,也被依次称为 OSI 第一层.第二层.⋯⋯. 第七层. 如下图: 各层功 ...

  2. Spring.Net+NHibernate+asp.net mvc + easyui

    毕业4个月的入手项目..前段时间在公司一直做的维护..为了弄明白自己也就跟着写了一个,目前也正在学习:不对的或者是有更好的还请各位赐教. 在学习的过程中主要参考::http://www.cnblogs ...

  3. html.day01

    1.web标准: 1. 结构  (xhtml)  2. 表现(css)  3.行为(js) html   超文本标记语言 xhtml  (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...

  4. CI 模型的使用M与C之间的调用

    CI是PHP一个比较轻,并且好用的一个框架 分层也是分的比较清晰的一个 这里先展示MODEL 放在application/models 目录下面user_model.php <?php clas ...

  5. Swift - 15 - 导入Foundation使用更多字符串功能

    //: Playground - noun: a place where people can play import Foundation var str = "Hello, playgr ...

  6. hdu1690 Bus System(最短路 Dijkstra)

    Problem Description Because of the huge population of China, public transportation is very important ...

  7. 【USACO 2.2.1】序言页码

    [题目描述] 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,以下是标准数字表: I 1 L 50 M 1000 V 5 C 100 X 10 D 500 最多3个同样的可以表 ...

  8. MPICH2在两台Ubuntu上安装(用mpd做进程管理)

    本文在经过大量的实验终于不负众望成功的在两台Ubuntu 12.04上部署MPI的一个小型集群,MPICH2所用版本为mpich2-1.4.1,下载地址:http://www.mcs.anl.gov/ ...

  9. asp.net使用Mysql乱码处理

    在asp.net与mysql数据库打交道的时候,由于配置的问题,会遇到自己写的方法在读取数据库中数据的时候,英文,数字可以正常通过,但是中文就无法通过,以登录为例(方法略),当输入英文用户名的时候可以 ...

  10. NoSQL数据库技术特性解析之文档数据库

    现今云计算的从业人员对NoSQL一词并不感到陌生,虽然很多技术人员都长期从事关系数据库的工作,但现在他们对NoSQL技术充满期待.对于企业来说,从关系型数据库到NoSQL数据库转变绝对是个需要深思熟虑 ...