diff --git a/Binary representation of next number b/Binary representation of next number new file mode 100644 index 0000000..e7be053 --- /dev/null +++ b/Binary representation of next number @@ -0,0 +1,22 @@ +class Solution { + public: + string binaryNextNumber(string s) { + // code here. + int i = s.size()-1; + while(i>=0 && s[i]!='0'){ + s[i] = '0'; + i--; + } + if(i != -1) + s[i] = '1'; + else + s = "1"+s; + i = 0; + while(s[i]=='0'){ + i++; + } + if(s[0] == '0') + s.erase(0, i); + return s; + } +};