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 = 111 + 1 = 2. Since 2 has only one digit, return it.

这道题是求一个数的数根。

数根有一个同余性质:一个数与它的数根对(b-1)同余(b是进制数)。

举个简单的例子就明白了:

123=1*100+2*10+3

=1*(99+1)+2*(9+1)+3

=(99+2*9)+(1+2+3)

前面一项能被9整除,后面的一项就是各个位上数的和。对1+2+3后得到的数,还是可以这么拆分,一直下去直到数根。

所以一个数与它的数根对(b-1)同余(b是进制数)。

对于本题,我们就利用这个性质求数根,因为数根是一位数,而且数根%9和num%9结果一样的,所以我们就直接num%9,但是我们这里求出来的数不是数根,数根是[0,9],而%9求出的是[1,8],所以我们加一个小的处理技巧:先减1,模了以后再加1.

class Solution {
public:
    int addDigits(int num) {
        return 1+(num-1)%9;
    }
};
时间: 2024-10-25 10:24:35

leetcode 258. Add Digits(数论)的相关文章

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 }; 就是数位根!

[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

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的各位

(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: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 No.258 Add Digits

LeetCode  Algorithum # title solution difficulty 258  Add Digits  java  easy         NO.258 (2015.10.26 16:09:00) Given a non-negative integer num,repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the pro

Leetcode刷题记录[python]——258 Add Digits

一.前言 做这题有个小收获,关于Digital root的解法,有个极方便的小公式: 二.题258 Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. class Solution(object): def addDigits(self, num): """ :type num: int :rtype: i

LeetCode Javascript解答 258. Add Digits

258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = function(num) { var b = (num-1) % 9 + 1 ; return b; }; //之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同. 性质说明 1.任何数加9的数字根还是它本身.(特殊情况num=0) 小学学加法的时候我们都明白

258. Add Digits(C++)

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