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 ...
随机推荐
- EBS查询默认应用用户,比如是否需要锁定、修改这些用户
/* Formatted on 2018/3/15 13:05:39 (QP5 v5.256.13226.35538) */ --查询默认应用用户,比如是否需要锁定.修改这些用户 SELECT ROW ...
- dll 显示调用
今天尝试写了一个简单的C++DLL,并且用另一个CPP调用它,啥都不说,先贴代码 1.DLL(冒泡算法) extern "C" 必须最左 _declspec(dllexport)和 ...
- Linux内核list/hlist解读
转自:http://blog.chinaunix.net/uid-20671208-id-3763131.html 目录 1. 前言 2 2. 通用宏 2 2.1. typeof 2 2.1.1. 定 ...
- 维京传奇第四季/全集Vikings迅雷下载
英文全名Vikings,第4季(2015). 本季看点:<维京传奇>当第四季开始时,Ragnar已经病入膏肓,随时可能丧命.毫无疑问,权力斗争异常激烈,谁都想在Ragnar死后获得最大利益 ...
- Fragment Transactions & Activity State Loss
转自:http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html The foll ...
- Eclipse断点调试(DBG)Android应用
1.添加断点 双击左侧边框便可添加断点,右击也能添加断点. 2.进入调试模式 点击虫子,然后选择工程运行,快捷键为单击F11 ,如果是正常运行就是Ctrl+F11 3.单步调试+跳到下一个断点 运行到 ...
- 100base-T
100Base-T是一种以100Mbps速率工作的局域网(LAN)标准,它通常被称为快速以太网标准,并使用两对UTP(非屏蔽双绞线)铜质电缆. 快速以太网 : 与10BASE-T的区别在于网络速率是1 ...
- Risc-V指令集
https://riscv.org/specifications/ Risc-V文档包括:用户层指令集文档和特权架构文档,下面这两个文件的官网链接. User-Level ISA Specificat ...
- go语言之进阶篇通过range遍历channel内容
1.通过range遍历channel内容 package main import ( "fmt" ) func main() { ch := make(chan int) //创建 ...
- Direct I/O,Synchronous I/O的概念
Direct I/O概念: Direct I/O is a way to avoid entire caching layer in the kernel and send the I/O direc ...