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 ...
随机推荐
- ASP.NET MVC中实现属性和属性值的组合,即笛卡尔乘积01, 在控制台实现
在电商产品模块中必经的一个环节是:当选择某一个产品类别,动态生成该类别下的所有属性和属性项,这些属性项有些是以DropDownList的形式存在,有些是以CheckBoxList的形式存在.接着,把C ...
- 【jvm】java查看内存使用jmap,jstat和jstack使用 ,docker启动服务下查看jvm使用情况
[声明,如果是docker启动的服务,可以在进入容器内后,再使用如下命令操作] [docker exec -it 容器ID /bin/bash 即可进入容器内] [如果不是docker启动的 ...
- ASP.NET MVC:模块化/插件式架构实现(转载)
I’ve recently spent quite a lot of time researching and prototyping different ways to create a plugi ...
- ifram 取父窗体的URL地址
var url=''; try { url = window.top.document.referrer ; } catch(M) { if (window.parent) ...
- linux find 10天内改动过的文件
find . -name "*.h" -mtime -10 -type f -print find . -regex ".*\.\(c\|h\)" -mtime ...
- 命令行界面 (CLI)、终端 (Terminal)、Shell、TTY的区别
虽然这个话题已是老生常谈,搜索一下应该也能找到大把的相关文章.不过难得提到了这方面,就趁此机会把我的理解写下来,一来看看我是不是真正理解了,二来看看我能不能把它们之间的区别讲得更加简明易懂. 0. 太 ...
- Useful JVM Flags – Part 8 (GC Logging)
The last part of this series is about garbage collection logging and associated flags. The GC log is ...
- 第二十五章 springboot + hystrixdashboard
注意: hystrix基本使用:第十九章 springboot + hystrix(1) hystrix计数原理:附6 hystrix metrics and monitor 一.hystrixdas ...
- Go语言之进阶篇mysql增 删 改 查
一.mysql操作基本语法 1.创建名称nulige的数据库 CREATE DATABASE nulige DEFAULT CHARSET utf8 COLLATE utf8_general_ci; ...
- Go语言之进阶篇获取文件属性
1.获取文件属性 示例: get_file_attribute.go package main import ( "fmt" "os" ) func main( ...