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 ...
随机推荐
- Maven 的依赖范围
Maven 在编译项目主代码的时候需要使用一套 classpath,在编译和执行测试的时候会使用另外一套 classpath.最后,实际运行 Maven 项目的时候,又会使用一套 classpath. ...
- TeX 家族(TeX, pdfTeX, XeTeX, LuaTeX, LaTeX, pdfLaTeX, XeLaTeX …)
TeX 家族 带有 TeX 的词,仅仅是本文就已经提到了 TeX, LaTeX, XeLaTeX.通常中国学生面对不了解意思的一群形近单词,都会有一种「本能的恐惧」(笑~).因此,「大神们」在为新手介 ...
- oracle数据库 部分函数的用法
select * from tab; //获取当前用户的数据库的所有表名 select sys_guid(),UserName from TESTLIKUI; //获取guid select sys_ ...
- spring 通过启动命令配置文件路径
公司使用dubbo开发,提供了很多的服务,每个服务中一些配置都是一样的,比如注册中心地址,公共码表库等一下配置,这样在部署每一个程序的时候,修改每一个服务的配置增加很多的工作量.且领导不想对程序有大的 ...
- 直线DDA,直线和圆的Bresenham算法
// DDA.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<Windows.h> #include<g ...
- linux 内网时间同步配置
在工作中,内网环境机器的时间会有所差异,在某些测试环境下需要一毫秒都不允许出现误差,但又不想同步外网时间,那我们可以选择一台机器作为时间服务器来供其他机器进行时间同步,例如每隔1分钟同步一次时间. 一 ...
- WinServer-文件共享端口
When turning on Firewall, please assure that “File and Printer Sharing” on current network profile i ...
- HashMap,HashSet
HashMap,HashSet 摘自:https://www.cnblogs.com/skywang12345/p/3310887.html#a1 目录 一. HashMap(键值对形式存取,键 ...
- css详解1
1.css的三种引入方式: 1.1.行内引入 <div style="color:red;">魔降风云变</div> <html> <he ...
- linux ssh_config和sshd_config配置文件学习
在远程管理linux系统基本上都要使用到ssh,原因很简单:telnet.FTP等传输方式是以明文传送用户认证信息,本质上是不安全的,存在被网络窃听的危险.SSH(Secure Shell)目前较可 ...