diff --git a/1404. Number of Steps to Reduce a Number in Binary Representation to One b/1404. Number of Steps to Reduce a Number in Binary Representation to One new file mode 100644 index 0000000..05f7969 --- /dev/null +++ b/1404. Number of Steps to Reduce a Number in Binary Representation to One @@ -0,0 +1,26 @@ +class Solution { +public: + int numSteps(string s) { + int ans = 0; + + while (s != "1") { + if (s.back() == '0') { + s.pop_back(); + } else { + int i = s.size() - 1; + while (i >= 0 && s[i] == '1') { + s[i] = '0'; + i--; + } + if (i >= 0) { + s[i] = '1'; + } else { + s = '1' + s; + } + } + ans++; + } + + return ans; + } +};