Recursive java solution


  • 1
    A
    public class Solution {
        public int addDigits(int num) {
    	    if (num < 10) {
    		    return num;
    	    }
    	    
    	    int sum = 0;
    	    while (num != 0){
    	    	   sum = sum + (num % 10);
    		   num = num / 10; 
    	    }
    	    
        	    return this.addDigits(sum);
        }
    }

  • 0
    W

    didn't the problem say "Could you do it without any loop/recursion in O(1) runtime?"


Log in to reply
 

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