[leetcode][PHP实现]Add Digits

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
class Solution
{
    public function addDigits($num) 
    {
        while ($num > 10)
        {
            $tmp = 0;
            while ($num > 0)
            {
                $tmp += $num % 10;
                $num = intval($num / 10);
            }
            $num = $tmp;
        }
        return $num;
    }
}
$solution = new Solution();
echo $solution->addDigits(381);
?>
Licensed under CC BY-NC-SA 4.0