您的位置:首页 > 编程语言 > C语言/C++

Leetcode 67. Add Binary (Easy) (cpp)

2016-07-14 17:30 363 查看
Leetcode 67. Add Binary (Easy) (cpp)

Tag: Math, String

Difficulty: Easy

/*

67. Add Binary (Easy)

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

*/
class Solution {
public:
string addBinary(string a, string b) {
int i = a.length(), j = b.length(), digit = 0;
string res;
while (i || j || digit) {
digit += (i ? a[(i--) - 1] - '0' : 0) + (j ? b[(j--) - 1] - '0' : 0);
res = char(digit % 2 + '0') + res;
digit /= 2;
}
return res;
}
};


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: