Java short recursion


  • 0
    public int minSteps(int n) {
        return helper(n);
    }
    private int helper(int n) {
        if (n==1) return 0;
        if (n==2) return 2;
        for (int i = 2; i<=(int)Math.sqrt(n); i++) {
            if (n%i==0){
                return i + helper(n/i);
            }
        }
        return n;
    }

Log in to reply
 

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