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 ...
随机推荐
- js和jquery给iframe src赋值的3种方法
js和jquery给iframe src赋值的3种方法 网页使用iframe嵌入网页时,有时候需要动态处理src的值,而不是写死的,所以我们需要知道如何给iframe src赋值,通常是使用js或 ...
- android 多线程(二)
1. 使用 AsyncTask 实现进度条 package com.test.network; import android.os.AsyncTask; import android.support. ...
- C++构造函数详解(复制构造函数 也是 拷贝构造函数)
构造函数是干什么的 该类对象被创建时,编译系统对象分配内存空间,并自动调用该构造函数,由构造函数完成成员的初始化工作,故:构造函数的作用:初始化对象的数据成员. 构造函数的种类 1 class Com ...
- 洛谷p2922[USACO08DEC]秘密消息Secret Message
题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...
- Travelling HDU - 3001
Travelling HDU - 3001 方法:3进制状态压缩dp(更好的方法是预处理出每个状态数字对应的y数组,然后用刷表,时间复杂度可以少一个n) #include<cstdio> ...
- Mysql查询语句的 where子句、group by子句、having子句、order by子句、limit子句
Mysql的各个查询语句 一.where子句 语法:select *|字段列表 from 表名 where 表达式.where子句后面往往配合MySQL运算符一起使用(做条件判断) 作用:通过限定 ...
- adb shell报错:error: insufficient permissions for device的解决办法
1.错误描述 执行 adb shell 时,报错如下; error: insufficient permissions for device 2.解决办法 1,终端执行 lsusb 结果如下,注意绿 ...
- 5款好用的mysql客户端
1. EMS SQL Manager for MySQL 是一款高性能MySQL数据库服务器系统的管理和开发工具.它支持从MySQL 3.23到6.0的任一版本,并支持最新版本的MySQL的特点,包括 ...
- Can't locate ExtUtils/MakeMaker.pm in @INC
Can't locate ExtUtils/MakeMaker.pm in @INC 解决办法:yum install perl-devel
- 如何在Ubuntu里安装Helm
Helm是什么?在战网上玩过暗黑破坏神2代的程序员们应该还记得,Helm是国度的意思. 而在计算机领域,Helm是什么? Helm是Kubernetes的一个包管理工具,有点像nodejs的npm,U ...