accepted but with doubt


  • 0
    N

    not sure why this works, but I think the commented line should be included

    public:
        int integerReplacement(int n) {
            if(n==0)return 0;
            int res = 0;
            if(n==INT_MAX){
                n--;
               // res=1;
            }
            if(n==INT_MIN){
                n++;
                //res=1;
            }
            while(n!=1){
                if(n==3) return res+2;
                if(n%2==0)
                    n=n/2;
                else{//odd
                    if(n%4==3){n++;}
                    else n--;
                }
                res++;
            }
            return res;
        }
    };```

Log in to reply
 

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