LeetCode OJ: 258. Add Digits數根
由上述說明可能會讓人寫出一個每個位數相加的程式吧,不過其實有公式可以解的:

所以程式碼就很短又很快啦。
1 2 3 4 5 6 | class Solution { public: int addDigits(int num) { return num - 9 * (floor( (num-1) / 9)); } }; |
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

1 2 3 4 5 6 | class Solution { public: int addDigits(int num) { return num - 9 * (floor( (num-1) / 9)); } }; |
留言