Easy To Read Java Solution (code is for humans to read)


  • 1
    T
    class Solution {
        public int addDigits(int num) {
            do {
                if (String.valueOf(num).length() == 1) return num;
                String numAsString = String.valueOf(num);
                int sumOfDigits = 0;
            
                for (int i = 0; i < numAsString.length(); i++) {
                    sumOfDigits += Character.getNumericValue(numAsString.charAt(i));
                }
                num = sumOfDigits;
            } while (String.valueOf(num).length() > 1);
            
            
            return num;
            
        }
    }
    

Log in to reply
 

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