我也来刷LeetCode——3、Add Digits

  唉,做完这道题目后,有种羞愧无比的感觉,果然还是读书太少。。。

  什么意思呢?就像这道题目一样,要求你编程计算出从1加到100的结果。于是你用循环从1累加到100,结果为5050。而大家都知道,从数学上的角度来解答,结果就是(a[1]+a[n])* n / 2。

  LeetCode上面的这道【Add Digits】题,其实是道数学题。它这个求解的结果,在数学上称为数根。

  原题大概意思是这样,给定一个正整数,循环累加它的各位,直到结果为一位数。如 num 是38,计算的过程是 3+8=11,1+1=2,2为一位数,即为结果。而实际上这个结果就是这个数的数根。

  一个数的数根,用数学公式来表达就是 f(n) =(n - 1) % 9 + 1,这个公式的证明不难,可以看这里

  最后~还是觉得很惭愧。。。

1 class Solution {
2 public:
3     int addDigits(int num) {
4         return (num -1) % 9 + 1;
5     }
6 };
时间: 2024-10-31 04:28:36

我也来刷LeetCode——3、Add Digits的相关文章

LeetCode:Add Digits - 非负整数各位相加

1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits 3.题目内容 英文:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 中文:有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和--直到最后得到一个仅1位的数字

[LeetCode][JavaScript]Add Digits

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

[leetcode] 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

【Leetcode】Add Digits

题目链接:https://leetcode.com/problems/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 digi

[LeetCode]64. 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

Java [Leetcode 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. 解题思路: 假设输入的数字是一个5位数字num,则num的各位

leetcode 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. 这道题是求一个数的数根. 数根有一个同余性质:一个数与它的数根对(b-1)

(LeetCode)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 an

(easy)LeetCode 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

leetcode 258 Add Digits数论

1 class Solution { 2 public: 3 int addDigits(int num) { 4 if(num == 0 ) return num; 5 else return num % 9 == 0 ? 9 : num % 9; 6 } 7 }; 就是数位根!