c++ recursive solution


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

Log in to reply
 

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