题目

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. VMware安装Centos 7,网络连接问题以及解决方案

    转自: https://www.cnblogs.com/owaowa/p/6123902.html 在这里表示万分感谢 在使用CentOS虚拟机后,出现了无法上网的情况,使用主机ping虚机地址可以p ...

  2. C#中自定义类数组和结构数组的使用

    如有雷同,不胜荣幸,若转载,请注明 C#中自定义类数组和结构数组的使用 最近在很多项目中发现很多时候给定的数组要实现某个逻辑或处理很是麻烦,一维数组,二维数组,,,等等需要经过n多转换,还不如自己写一 ...

  3. Swift dynamic关键字

    使用dynamic关键字标记属性,使属性启用Objc的动态转发功能: dynamic只用于类,不能用于结构体和枚举,因为它们没有继承机制,而Objc的动态转发就是根据继承关系来实现转发. 参考资料: ...

  4. htm5 + ajax 文件上传

    好文 http://www.cnblogs.com/morlin/p/4930822.html 后台接收 FormData 的参数一直为空,将jquery改为最高版本,问题解决.测试发现IE10以上才 ...

  5. MyEclipse常用快捷键及快捷键大全

    MyEclipse常用快捷键:alt+/     代码提示ctrl+shift+F   代码排版ctrl + /     注释当前行 ctrl+D      删除当前行 Alt+C       拷贝当 ...

  6. 使用Appache部署WEB服务器

    Apache的起源(这个就不说了,百度下就都有了) 简介:Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行, ...

  7. Apache Kylin的框架介绍

    不多说,直接上干货! Apache kylin 能提供低延迟(sub-second latency)的秘诀就是预计算,即针对一个星型拓扑结构的数据立方体,预计算多个维度组合的度量,然后将结果保存在hb ...

  8. 在虚拟机里安装windows或Linux系统时,安装窗口过大按钮有时点不到解决办法(图文详解)

    不多说,直接上干货! 问题详情 解决办法 很简单快捷的解决办法,就是快捷键ALT+F7,可以拖动窗口的位置. 成功!

  9. C#程序A调用程序B的问题

    C#程序A调用程序B,如果程序B中存在 string path1 = System.Environment.CurrentDirectory; 程序A中开启B进程的代码为: System.Diagno ...

  10. 用vue做一个酷炫的menu

    写在前面 最近看到一个非常酷炫的menu插件,一直想把它鼓捣成vue形式,谁让我是vue的死灰粉呢,如果这都不算爱