vendredi 3 juin 2016

following is the code to convert a number to binary string .how the ans.push_back((char)('0' + rem)) is working?

class Solution { public: string findDigitsInBinary(int n) { string ans; if (n == 0) return "0";

        while (n > 0) {
        int rem = n % 2; 
        ans.push_back((char)('0' + rem));
        n /= 2;
        }

        reverse(ans.begin(), ans.end());
        return ans;
    }
};

Aucun commentaire:

Enregistrer un commentaire