题目

Given two binary strings, return their sum (also a binary string).

For example,

a = “11”

b = “1”

Return “100”.

分析

一个简单的字符串相加,该题目要注意两点:

  1. 字符位的求和计算,必须转换为整型,即:

    如下利用‘0’字符作为中间转换,才得到正确结果。

    int temp = (a[i]-'0') + (b[i]-'0');
    
    char c = temp + '0';
  2. 进位保存于计算

AC代码

class Solution {
public:
string addBinary(string a, string b) {
//首先,求得两个字符串的长度
int la = strlen(a.c_str());
int lb = strlen(b.c_str()); //若其中一个字符串为空,直接返回另一个字符串即可
if (la == 0)
return b;
else if (lb == 0)
return a; //保存进位
int carry = 0 ;
//保存结果
string r = la > lb ? a : b ;
int k = la > lb ? la - 1 : lb - 1;
//循环变量
int ia = la - 1, ib = lb - 1;
for (; ia >= 0 && ib >= 0; --ia, --ib)
{
//转换为整数计算
int temp = (a[ia] - '0') + (b[ib] - '0') + carry;
if (temp >= 2)
{
temp -= 2;
carry = 1;
}
else{
carry = 0;
}//if
//保存结果为相应字符类型
r[k] = temp + '0';
--k;
}//while while (ia >= 0)
{
int temp = (a[ia] - '0') + carry;
if (temp >= 2)
{
temp -= 2;
carry = 1;
}
else{
carry = 0;
}//if
r[k] = temp + '0';
--ia;
--k;
}//while while (ib >= 0)
{
int temp = (b[ib] - '0') + carry;
if (temp >= 2)
{
temp -= 2;
carry = 1;
}
else{
carry = 0;
}//if
r[k] = temp + '0';
--ib;
--k;
} //若首位也有进位,则用"1"链接
if (carry == 0)
return r;
else{
return "1"+r;
} }
};

GitHub测试程序源码

LeetCode(67) Add Binary的更多相关文章

  1. LeetCode(258) Add Digits

    题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one dig ...

  2. LeetCode(114) Flatten Binary Tree to Linked List

    题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...

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

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

  5. LeetCode(67):二进制求和

    Easy! 题目描述: 给定两个二进制字符串,返回它们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = " ...

  6. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  7. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  8. LeetCode(24)-Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  9. LeetCode(99) Recover Binary Search Tree

    题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...

随机推荐

  1. hdu 5409 CRB and Graph(边双联通分量)

    题意: 给一个图一些边,保证图连通 问对于每条边,如果去除该边后使得图中一些点不连通.设这些点(u,v),要求使u尽量小,v尽量大,输出这样的(u,v).否则输出0 0. #include <b ...

  2. Sereja and Brackets(括号匹配)

    Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length  ...

  3. 1-8继承extends

    什么是继承? 继承是面向对象三大特征之一.java中的继承描述的是两个类之间的关系,被继承的类称为父类,继承的类称为子类,使用extends关键字来表示.在java语言里面只支持单继承,即一个类只能有 ...

  4. c#学习系列之关键字where

    where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量.    1.接口约束.         例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就 ...

  5. JVM 内存机制理解【转自http://www.cnblogs.com/dingyingsi/p/3760447.html】

    我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...

  6. 虚方法virtual详解

    虚方法virtual详解   从C#的程序编译的角度来看,它和其它一般的函数有什么区别呢?一般函数在编译时就静态地编译到了执行文件中,其相对地址在程序运行期间是不发生变化的,也就是写死了的!而虚函数在 ...

  7. Some Python Tricks

    python 的包管理很不好用,理解费力,故偷懒,模块仍按文件布局,写一个合并脚本将各个模块合并输出到一个脚本文件,分别管理,合并输出,回避了加载模块的问题 f-format 仅在python 3.6 ...

  8. 前端之CSS语法及选择器

    一.css语法: css由两大部分组成:选择符和声明,声明由属性和属性值两部分组成; 选择符{属性:属性值;属性:属性值;} 注: a) 属性和属性值之间用冒号连接: b)每条声明结束要加分号: 二. ...

  9. AJPFX总结List的三个子类的特点

    ArrayList:                        底层数据结构是数组,查询快,增删慢.                        线程不安全,效率高.              ...

  10. canvas防画图工具

    <style> body {   background: black;   text-align: center; } #cans {   background: white; } < ...