LeetCode Solutions

258. Add Digits

Time: $O(1)$

Space: $O(1)$

			

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

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

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