Skip to content

Commit

Permalink
Create Binary representation of next number
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhiupman568 authored Jun 4, 2024
1 parent 6c41d0a commit dc6d3bb
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Binary representation of next number
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit dc6d3bb

Please sign in to comment.