Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和……直到最后得到一个仅1位的数字(即小于10的数字)。
例如:num=38,3+8=11,1+1=2。因为2小于10,因此返回2。
/*
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?
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solution {
public class Solution {
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;
}
}
//one line solution
public int AddDigits(int num) {
return (num - 1) % 9 + 1;
}
}
class Program {
static void Main(string[] args) {
var s = new Solution();
var res = s.AddDigits(38);
Console.WriteLine(res);
}
}
}