Leetcode

Add Digits

  • Time:O(1)
  • Space:O(1)

C++

class Solution {
 public:
  int addDigits(int num) {
    return 1 + (num - 1) % 9;
  }
};

JAVA

class Solution {
  public int addDigits(int num) {
    return 1 + (num - 1) % 9;
  }
}

Python

class Solution:
  def addDigits(self, num: int) -> int:
    return 0 if num == 0 else 1 + (num - 1) % 9