[LeetCode] Add Digits

258. Add Digits

Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

1
2
3
4
5
6
7
class Solution {
public:
int addDigits(int& n) {
return n ? 1 + (n - 1) % 9: 0;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/08/PS/LeetCode/add-digits/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.