本文主要是介绍Easy 15 Add Binary(67),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Description
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
Solution
class Solution {
public:string addBinary(string a, string b) {string res="";int c=0,i=a.size()-1,j=b.size()-1;while(i>=0||j>=0||c==1){c+= (i>=0)? a[i--]-'0':0;c+= (j>=0)? b[j--]-'0':0;res=char(c%2+'0')+res;c/=2;}return res;}
};
这篇关于Easy 15 Add Binary(67)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!