Add Digits Solutions in C++
Number 258
Difficulty Easy
Acceptance 57.7%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/add-digits/// Author : Timothy Lim, Hao Chen// Date : 2015-10-1class Solution {public:int addDigits(int num) {switch(random()%5+1){case 1: return addDigits01(num);case 2: return addDigits02(num);case 3: return addDigits03(num);case 4: return addDigits04(num);default: return addDigits05(num);}}//regualr wayint addDigits01(int num) {while(num > 9) {int sum;for(sum=0; num > 0; sum += num%10 , num/=10);num = sum;}return num;}//This solution looks is very tricky, but acutally it is easy to understand.//it just keep adding the last digital until the num < 10int addDigits02(int num) {while(num > 9) {num = num / 10 + num % 10;}return num;}// Let's observe the pattern// 1 1// 2 2// ... ...// 8 8// 9 9// 10 1// 11 2// 12 3// ... ...// 17 8// 18 9// 19 1// 20 2// ... ...// It looks most of number just simply %9 is the answer,// but there are some edge cases.// 9%9=0 but we need 9.// 18%9=0 but we need 9// so we can find the solution is:// 1) num <=9, return num// 2) num > 9, reutrn num%9 if num%9>0// return 9 if num%9 ==0int addDigits03(int num) {return num >9 ? ((num %9)==0 ? 9:num%9) : num;}//But actually, we can use (num-1)%9 + 1 to make all cases right.int addDigits04(int num){return (num - 1) % 9 + 1;}//This solution is similar with pervious solution.int addDigits05(int num){return num - 9 * ((num - 1)/9);}};