My solutions of adding digits


  • 0
    B
    public class Solution {
        public int addDigits(int num) {
            int d = 1;
            int[] s = new int[11];
            int i = 0;
            for(i = 10; i >= 0; i--){
                d = (int)Math.pow(10,i);
                s[10-i] = num / d;
                num -= d*s[10-i];
            }
            int r = 0;
            for(i = 0; i <=10; i++){
                r += s[i];
                if (r >= 10){
                    r -= 9;
                }
               
            }
            return r;
        }
    }

Log in to reply
 

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