Lintcode: Add Binary
C++
class Solution {
public:
/**
* @param a a number
* @param b a number
* @return the result
*/
string addBinary(string& a, string& b) {
// Write your code here
if (a == "") {
return b;
}
if (b == "") {
return a;
}
string result;
int carry = ;
int ai, bj, sum;
char val;
for (int i = a.size()-, j = b.size()-; i >= || j >= ; i--, j--) {
ai = i >= ?a[i]-'':;
bj = j >= ?b[j]-'':;
sum = ai+bj+carry;
val = sum%?'':'';
carry = sum/;
result.insert(result.begin(), val);
}
if (carry == ) {
result.insert(result.begin(), '');
}
return result;
}
};
Lintcode: Add Binary的更多相关文章
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- 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 ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
随机推荐
- AngularJS订阅API服务
本篇使用AngularJS实现订阅某个API服务. 首页大致是: 其中,what's on显示首页内容,Search通过输入关键词调用API服务显示到页面,MyShows显示订阅的内容. Sarch页 ...
- Unity3D实践系列07,组件的启用或禁用开关,物体的的可见或不可见开关,以及相应事件
创建一个Unity项目. 在"Project"窗口中,在"Asserts"中,添加"_MyScene"文件夹. 点击"File&q ...
- struct对象可能分配在托管堆上吗
struct对象可能被分配在托管堆上吗? --会的. 比如当对struct装箱的时候,就会被分配在托管堆上. 比如,让一个struct实现一个接口. public interface IReport ...
- Delphi 文件遍历
unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- iReport数据库连接找不到驱动
- docker 日志清理与设置
清空 cat /dev/null >json.log docker-compose nginx: image: nginx:1.12.1 restart: always logging: dri ...
- centos7 开机启动管理
查看开机启动项 ls /etc/systemd/system/multi-user.target.wants/ 设置开机启动 systemctl enable mongodb.service
- Notification详解(含工具类)
昨天一天只写了两篇文章,效率超低.追其原因呢,其实我一直在研究noti ...
- Gallery和自定义Adapter配合使用,实现图片预览
Gallery是一个可以拖动的列表,正中对应的是选中的东西.他和spinner有共同的父类:AbsSpinner 属性: android:animationDuration="1000&qu ...
- 反向解析与PTR(Pointer Record)
PTR记录,是电子邮件系统中的邮件交换记录的一种:另一种邮件交换记录是A记录(在IPv4协议中)或AAAA记录(在IPv6协议中).PTR记录常被用于反向地址解析. PTR记录 Pointer ...