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 +(num-)%;
}
};

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

  1. Leetcode 258 Add Digits数论

    class Solution { public: int addDigits(int num) { ) return num; == ? : num % ; } }; 就是数位根!

  2. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  3. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  4. [LeetCode] 258. Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  6. (easy)LeetCode 258.Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  7. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  8. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  9. LeetCode: 258 Add Digits(easy)

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

随机推荐

  1. Linux与本地上传下载文件

    当出于安全原因,客户的服务器不允许使用windows第三方插件连接的时候,本地windows跟远程的linux服务器进行文件的上下传受到了限制,此时可以在linux服务器上安装一个工具:lrzsz.安 ...

  2. Android屏幕密度(Density)和分辨率概念详解

    移动设备有大有小,那么如何适应不同屏幕呢,这给我们编程人员造成了很多困惑.我也是突然想到这些问题,然后去网上搜搜相关东西,整理如下.   首先,对下面这些长度单位必须了解. Android中的长度单位 ...

  3. bzoj1185【HNOI2007】最小矩形覆盖

    1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special Judge Submit: 1114  Solv ...

  4. Leetcode - CopyWithRandomList

    Algorithm: Iterate and copy the original list first. For the random pointer, just copy the value fro ...

  5. SVN流程图协作图

  6. 【BZOJ3230】相似子串 后缀数组+二分+RMQ

    [BZOJ3230]相似子串 Description Input 输入第1行,包含3个整数N,Q.Q代表询问组数.第2行是字符串S.接下来Q行,每行两个整数i和j.(1≤i≤j). Output 输出 ...

  7. Python高级编程第二版--笔记

    不只是CPython Stackless Python Jython(与java集成) IronPython(与net集成) PyPy python真正出众的领域在于围绕语言打造的整个生态系统. Py ...

  8. 【网络与系统安全】20179209 wireshark和nmap实验

    TCP三次握手包 在进行实验前,先梳理一遍TCP三次握手的过程,这个图是我本科学网络时画过不少于十遍的图,我觉得非常有纪念意义. 稍作解释,第一次握手,客户端发起请求连接,发送一个标志位为SYN的ip ...

  9. pinpoint agent线程模型

    pinpoint agent线程模型 以下分析基于pinpoint1.7.1版本 pinpoint agent主要使用到的异步线程有4个 DeadlockMonitorThread : 死锁监测线程, ...

  10. HDU - 1272 小希的迷宫 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 思路 只需要判断 这张图 无环 并且只有一个连通块 就可以了 要注意 如果 只输入 0 0 那给 ...