LeetCode67 Add Binary
题目:
Given two binary strings, return their sum (also a binary string). (Easy)
For example,
a = "11"
b = "1"
Return "100".
分析:
思路很简单,就是按位依次相加,处理进位即可,循环条件里面可以写上carry == 1,用一个完整循环解决问题,而不用循环结束后再多加判定。
注意:题目给的是string,注意处理char和int转换。
代码:
class Solution {
public:
string addBinary(string a, string b) {
string result = "";
int i = a.size() - , j = b.size() - , carry = ;
while (i >= || j >= || carry == ) {
int temp = carry;
carry = ;
if (i >= ) {
temp += a[i] - '';
i--;
}
if (j >= ) {
temp += b[j] - '';
j--;
}
if (temp == ) {
carry = ;
temp = ;
}
if (temp == ) {
carry = ;
temp = ;
}
result = char('' + temp) + result;
}
return result;
}
};
LeetCode67 Add Binary的更多相关文章
- LeetCode----67. Add Binary(java)
package addBinary67;/* Given two binary strings, return their sum (also a binary string).For example ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- Add Binary
Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 2016.6.21——Add Binary
Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
随机推荐
- android的AIDL
一.AIDL的意义: AIDL全称是Android Interface Definition Language,是android接口定义语言.AIDL就是为了避免我们一遍遍的写一些 ...
- 关于 linux 的 limit 的设置
以下内容参考链接 1.file-max系统最大打开文件描述符数 永久性:在/etc/sysctl.conf中设置 2.nr_open是单个进程可分配的最大文件数. 内核支持的最大file handle ...
- 华为RH2288 V3服务器进入BIOS并设置iBMC地址
服务器重启后 第一个画面 提示Ctrl+H或Ctrl+S进入相关配置 其中, Ctrl+H进入WebBIOS初次安装系统可在此做RAID配置 此画面不做操作,等待跳过 进入第二画面 可以选择BootM ...
- 模拟15 题解(waiting)
T1 60%算法 定义f[i][j]表示枚举到i位置,已经使用过了j个队, $f[i][j]+=f[i-1][t] ( t \in [max(0,j-k),j])$滚动一下 这是个O(n^3)的,考虑 ...
- Django项目:CRM(客户关系管理系统)--16--08PerfectCRM实现King_admin显示注册表的字段表头
# king_urls.py # ————————02PerfectCRM创建ADMIN页面———————— from django.conf.urls import url from king_ad ...
- web服务发展历程
PhP发展历史1.php: 开始名字含义:personal home page 个人网页 现在名字含义:HyperText Perprocessor 超文本预处理语言 预处理: 说明PHP是在服务器预 ...
- 转搞定python多线程和多进程
转自https://www.cnblogs.com/whatisfantasy/p/6440585.html 1 概念梳理: 1.1 线程 1.1.1 什么是线程 线程是操作系统能够进行运算调度的最小 ...
- Spring Boot → 09:使用外置Servlet容器_tomcat9.0
Spring Boot → 09:使用外置Servlet容器_tomcat9.0
- 软工作业——Alpha版本第一周小结
姓名 学号 周前计划安排 每周实际工作记录 自我打分 zxl 061425 1.进行任务分析2.进行任务分配 1.对任务进行了初步的划分,但还为进行给模块间的联系2.给每人分配了任务3.负责扫码签到功 ...
- .net core/.net 使用 CommandLineParser 来标准化地解析命令行
CommandLineParser 是一款用于解析命令行参数的 NuGet 包.你只需要关注你的业务,而命令行解析只需要极少量的配置代码. 本文将介绍如何使用 CommandLineParser 高效 ...