258.整形每个组成数字相加 Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
public int AddDigits(int num) {char[] nums = num.ToString().ToCharArray();if (num / 10 >= 1) {int sum = 0;foreach (char c in nums) {sum += (Convert.ToInt32(c) - 48);}return AddDigits(sum);} else {return num;}}

浙公网安备 33010602011771号