标签:
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) { string ans=""; int c=0,i=a.length()-1,j=b.length()-1; while(i>=0||j>=0||c>0) { c+= i>=0? a[i--]-‘0‘:0; c+= j>=0? b[j--]-‘0‘:0; ans=char(c%2+‘0‘) + ans; c/=2; } return ans; } };
leetcode 67. Add Binary (高精度加法)
标签:
原文地址:http://www.cnblogs.com/zywscq/p/5401142.html