C++


  • 0
    G
    class Solution {
    public:
        int integerReplacement(int n) {
            int res;
            res = replace(n);
            return res;
        }
        
        long long replace(long long n) {
            
            if(n <= 1) {
                return 0;
            }
            if(n % 2 == 0) {
                return replace(n/2)+1;
            }
            else {
                return min(replace(n+1), replace(n-1))+1;
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.