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; } };
时间: 2024-10-11 06:39:45