Lazy and easy Java solution with recursion


  • 0
    T

    public class Solution {
    public int integerReplacement(int n) {
    if(n==2147483647) return 2+integerReplacement(1073741824);
    if(n<2) return 0;
    if(n%2==0) return 1+ integerReplacement(n/2);
    else return 2+Math.min(integerReplacement((n-1)/2),integerReplacement((n+1)/2));
    }
    }


Log in to reply
 

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