6ms java solution


  • 0
    Z
    public int integerReplacement(int n) {
            int count = 0;
            while(n!=1){
                count++;
                if(n%2==0){
                    n = n/2;
                }else{
                    int temp1 = (n-1)/2;
                    int temp2 = temp1+1;
                    int temp = 0;
                    if(temp1%2==0 || temp1==1){
                        temp = temp1;
                    }else{
                        temp = temp2;
                    }
                    n = temp;
                    count++;
                }
            }
            return count;
        }
    

Log in to reply
 

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