67. Add Binary
发布时间:2020-12-14 04:30:33 所属栏目:大数据 来源:网络整理
导读:https://leetcode.com/problems/add-binary/description/ class Solution { public : string addBinary( string a, string b) { string res; int carry = 0 ; for ( int ia = a.length() - 1 ,ib = b.length() - 1 ; ia = 0 || ib = 0 ; ia--,ib-- ) { int c
https://leetcode.com/problems/add-binary/description/ class Solution { public: string addBinary(string a,string b) { string res; int carry = 0; for (int ia = a.length() - 1,ib = b.length() - 1; ia >= 0 || ib >= 0; ia--,ib--) { int ca = ia < 0 ? 0 : a[ia] - ‘0‘; int cb = ib < 0 ? 0 : b[ib] - ‘0‘; int c = ca + cb + carry; res.push_back(c % 2 + ‘0‘); carry = c / 2; } if (carry > 0) res.push_back(carry + ‘0‘); reverse(res.begin(),res.end()); return res; } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |