leetcode解题报告(26):Add Binary
描述
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
分析
这道题没做出来。。
逛了评论区才写出来的,代码写得很简洁
https://discuss.leetcode.com/topic/8981/short-code-by-c
我之前的思路是对两个string的每一位都做处理,比如,如果两个都是1,carry就为1,tmp(当前位的值)就为0;如果只有一个1,carry就为0,tmp为1;如果全为0,carry和tmp就都为0.
然后发现来自低位的进位要先初始化一次,于是就对最低位单独处理,得到carry的初始值。这样写的话太麻烦了,于是转而求助讨论区。
我参照的代码只用一个变量carry来同时处理进位和当前位。由于string只包含0和1,因此每次都将两个string的当前位变为整型加到carry变量。
比较有趣的地方是while循环的第三个条件,这个条件只有在两个string都遍历结束时才会用到(也就是判断最高位是否有进位),如果有进位,那么carry的值是为1,条件成立,直接把这个1加到最后。
由于整个操作都是直接往待返回的string的末尾加,因此要调用reverse函数逆置。
代码如下:
class Solution {
public:
string addBinary(string a, string b) {
string ret;
int i = a.size() - 1;
int j = b.size() - 1;
int carry = 0;
while(i >= 0 || j >= 0
|| carry > 0){ //仅用来处理最高位有进位的情况
if(i >= 0){
carry += a[i] - '0';
--i;
}
if(j >= 0){
carry += b[j] - '0';
--j;
}
ret += (carry % 2) + '0';
carry /= 2;
}
reverse(ret.begin(),ret.end());
return ret;
}
};
leetcode解题报告(26):Add Binary的更多相关文章
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
- LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
- LeetCode解题报告:Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
随机推荐
- quartz2.3.0(十)xml配置方式定义quartz定时任务
1.新增pom依赖 除了按照<quartz2.3.0系列目录——带您由浅入深全面掌握quartz2.3.0>添加依赖之外,pom.xml里新增加依赖: <dependency> ...
- 树卷积神经网络Tree-CNN: A Deep Convolutional Neural Network for Lifelong Learning
树卷积神经网络Tree-CNN: A Deep Convolutional Neural Network for Lifelong Learning 2018-04-17 08:32:39 看_这是一 ...
- C# ——Parallel类
一.Parallel类 Parallel类提供了数据和任务的并行性: 二.Paraller.For() Paraller.For()方法类似于C#的for循环语句,也是多次执行一个任务.使用Paral ...
- Java开发环境搭建(一)
一.JDK与JRE JDK:Java Development Kit,Java开发工具包,是给开发人员使用的,其中包含了Java的开发工具,如java.javac.jar等命令,同时也包含了JRE. ...
- 基于JMeter的Quick Easy FTP Server性能测试
FTP性能测试 1.引言 1.1背景说明 本测试选用的是一个小型的FTP服务器软件:Quick Easy FTP Server.Quick Easy FTP Server是一个全中文的FTP服务器软件 ...
- 超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用)
超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用) 转载自:https://www.jianshu.com/p/2ad8c8b5bf75 亲测有效~ <tem ...
- windows下git创建本地分支并建立对应远程分支
在对应项目目录下打开命令提示符 git branch -a 查看所有本地和远程分支 git checkout -b [newBranch] 建立本地分支newBranch git p ...
- HTML5 结构标签
一.定义标题栏:header header 元素是一种具有引导和导航作用的结构元素,通常用来放置整个页面或页面内的一个内容区块的标题,但也可以包含其他内容,因此整个页面的标题应该放在页面的开头. he ...
- Solr-rce历史漏洞复现
最近Solr又出了一个RCE漏洞,复现了一下 # coding: utf-8 import requestsimport argparsefrom urllib import parse if __n ...
- jenkins安装和简单部署
jenkins安装和简单部署 jenkins历史 jenkins是一款非常好用的团队CI(Continuous Integration)工具.它可以使你的构建,集成,发布,开发流程自动化.减轻各个环节 ...