Skip to content

258. Add Digits

  • Time: $O(1)$
  • Space: $O(1)$
1
2
3
4
5
6
class Solution {
 public:
  int addDigits(int num) {
    return 1 + (num - 1) % 9;
  }
};
1
2
3
4
5
class Solution {
  public int addDigits(int num) {
    return 1 + (num - 1) % 9;
  }
}
1
2
3
class Solution:
  def addDigits(self, num: int) -> int:
    return 0 if num == 0 else 1 + (num - 1) % 9