2017年5月7日 16:01:4067. Add Binary【easy】

xiaoxiao2021-02-28  92

Given two binary strings, return their sum (also a binary string).

For example, a = "11" b = "1" Return "100".

Subscribe to see which companies asked this question.

class Solution {

public:     string addBinary(string a, string b) {         string s = "";                  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;             s = char(c % 2 + '0') + s;             c /= 2;         }                  return s;     }

};

有点难看懂

转载请注明原文地址: https://www.6miu.com/read-76351.html

最新回复(0)