@@ -0,0 +1,874 @@
+/*%0AThis question is from https://leetcode.com/problems/add-digits/%0ADifficulty: easy %0A%0AGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.%0A%0AFor example:%0A%0AGiven num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.%0A%0AFollow up:%0ACould you do it without any loop/recursion in O(1) runtime?%0A*/%0A%0Apublic class solution%7B%0A//1234, 1+2+3+4 = 10, 1+0 = 1%0A//567, 5+6+7 = 18, 1+8 = 9%0A//999, 9+9+9 = 27, 2+7 = 9%0A//1111, 1+1+1+1 = 4%0A// 38 = 100110%0A// 39, 3+9 = 12, 1+2 = 3 ; 39%259 = 3%0A// 40, 4+0 = 4 ; 40%259 = 4%0A// 41, 4+1 = 5 ; 40%259 = 5%0A// 42, 4+2 = 6%0A// 43, 4+3 = 7%0A// 44, 4+4 = 8%0A// 45, 4+5 = 9%0A// 46, 4+6 = 10, 1+0=1%0A// 47, 4+7 = 11, 1+1=2 %0A public int addDigits(int num)%7B%0A %09if(num !=0)%7B%0A %09%09int ans = num%259;%0A %09%09if(ans == 0)%7B%0A %09%09 return 9;%0A %09%09%7D else%7B%0A %09%09return ans;%0A %09%09%7D%0A %09%7Delse%7B%0A %09%09return 0;%0A %09%7D%0A %7D%0A%0A%7D

258. Add Digits