LeetCode(67) Add Binary
题目
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
分析
一个简单的字符串相加,该题目要注意两点:
字符位的求和计算,必须转换为整型,即:
如下利用‘0’字符作为中间转换,才得到正确结果。int temp = (a[i]-'0') + (b[i]-'0'); char c = temp + '0';进位保存于计算
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;
}
}
};
LeetCode(67) Add Binary的更多相关文章
- LeetCode(258) Add Digits
题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one dig ...
- LeetCode(114) Flatten Binary Tree to Linked List
题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...
- 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 ...
- 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 ...
- LeetCode(67):二进制求和
Easy! 题目描述: 给定两个二进制字符串,返回它们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = " ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- LeetCode(24)-Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode(99) Recover Binary Search Tree
题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...
随机推荐
- mybatis使用要点(2019.5.19)
接口入参 只有一个参数,叫啥都没问题 有两个参数以上,需使用@Param,否则名字依次为0.1.2和param1.param2.param3 一般用#,防sql注入:偶尔用$,比如需要动态表名等 接口 ...
- Codeforces Round #302 (Div. 1) 训练
链接: http://codeforces.com/contest/543 过程: 惨淡的只做出了A和C 题解: A 题解: 简单的一道题 我们用$dp[i][j]$表示当前考虑到前num个人(这个另 ...
- 507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...
- qconshanghai2014
主题演讲 容器化的云——CohesiveFT首席技术官 Chris Swan 建设强大的工程师文化——Spotify工程总监 Kevin Goldsmith 软件项目变更的管理和生存之道——jClar ...
- 解决IDEA Tomcat控制台乱码问题
1.在Tomcat Server的配置中添加一句命令: 神秘代码: -Dfile.encoding=UTF-8 重启Tomcat,ok. 如果还不行,则需要: 1.在Settings中修改文件编码 2 ...
- Spring-bean(一)
配置形式:基于xml文件的方式:基于注解的方式 Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 依赖注入的方式:属性注入,构造器注入 ...
- Android原生系统API自带dp、px、sp单位转换
Android系统中自带的Api中可以使用TypedValue进行单位转换 1,调用系统api转换单位 // 获得转换后的px值 float pxDimension = TypedValue.appl ...
- java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()
原因:hibernate-jpa-2.0-api-1.0.0.Final.jar.ejb3-persistence.jar中的javax.persistence与javaEE 5 Librares中的 ...
- .net4.5注册到iis
开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windir%\Microsoft.NET\Framework\v4.0.30319\as ...
- 【转载】SQL Server 2012 日志传送
SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件夹,主机备份放在这个文件夹,而且客户机有权访问这个共享文件夹. 二.基本配置 1.启动配 ...