leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

add-digits.js (685B)


      1 //Add the digits of an inputted integer until
      2 //the output is a number 1-9 then return that value.
      3 //The time complexiy of this code is O(log(n)) where n is the input number
      4 //The true time complexity is O(log10N) where the output would be the number
      5 //of digits in the value, but since we are talking big O notation the constant
      6 //does not get factored in.
      7 //Time: 61ms Beats: 93.60%
      8 //Memory: 43.3MB Beats: 92.76%
      9 
     10 var addDigits = function(num) {
     11     while(num >= 10){
     12         num = add_vals(num);
     13     }
     14     return num;
     15 };
     16 
     17 function add_vals(num) {
     18     let ret_val = 0;
     19     while(num > 0){
     20         ret_val += num % 10;
     21         num = Math.floor(num / 10);
     22     }
     23     return ret_val;
     24 }