Skip to content

Commit db44b4c

Browse files
authored
Create 1404. Number of Steps to Reduce a Number in Binary Representat… (#491)
2 parents 89cdd7a + 6fc13d0 commit db44b4c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int numSteps(string s) {
4+
int ans = 0;
5+
6+
while (s != "1") {
7+
if (s.back() == '0') {
8+
s.pop_back();
9+
} else {
10+
int i = s.size() - 1;
11+
while (i >= 0 && s[i] == '1') {
12+
s[i] = '0';
13+
i--;
14+
}
15+
if (i >= 0) {
16+
s[i] = '1';
17+
} else {
18+
s = '1' + s;
19+
}
20+
}
21+
ans++;
22+
}
23+
24+
return ans;
25+
}
26+
};

0 commit comments

Comments
 (0)