标签:class log string reverse end car begin ever size
class Solution { public: string addBinary(string a, string b) { int n = a.size() > b.size() ? a.size() : b.size(); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int carry = 0; string result = ""; for(int i=0; i<n; ++i) { int numa = i<a.size() ? (a[i] - ‘0‘) : 0; int numb = i<b.size() ? (b[i] - ‘0‘) : 0; int ret = (numa + numb + carry)%2; carry = (numa + numb + carry)/2; result.insert(0, to_string(ret)); } if(carry) result.insert(0, to_string(carry)); return result; } };
标签:class log string reverse end car begin ever size
原文地址:http://www.cnblogs.com/chengyuz/p/6748229.html