258 Add Digits

Myth丶恋晨 2022-08-04 10:50 237阅读 0赞
  1. public int addDigits(int num) {
  2. while (num > 9) {
  3. num = func(num);
  4. }
  5. return num;
  6. }
  7. public static int func(int n) {
  8. int temp = 0;
  9. while (n > 0) {
  10. temp += (n % 10);
  11. n = n / 10;
  12. }
  13. return temp;
  14. }

没有实现:
Follow up:
Could you do it without any loop/recursion in O(1) runtime?

发表评论

表情:
评论列表 (有 0 条评论,237人围观)

还没有评论,来说两句吧...

相关阅读