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. 程序员之---C语言细节19(来找茬,由/* */ 引起的凝视错误)

    主要内容:由/* */ 引起的凝视错误 有4处凝视错误 #include <stdio.h> #define N 10 //使用以下宏定义的凝视 #define BSC // #defin ...

  2. saltstack之用户管理

    1.添加用户 /srv/salt/top.sls base: 'test82.salt.cn': - user.useradd /srv/salt/user/useradd.sls jim: user ...

  3. Outlook2016删不掉主账户的解决方法

    控制面板>账户>邮件把配置文件删了 前两项和Outlook内部打开账户选项一样没用, 进第三个 重启Oulook的时候会提示重新建一个配置,就OK了!

  4. 安卓常用的xml控件配件的使用包含shape,declare-styleable、selector

    今天就讲我所遇到的常用到的一些通过xml文件制作的背景效果,以后用到的话就直接使用啦!哈哈,我一向就是这么懒! 接下来,就开始介绍了 1.shape的使用,可以混合使用 xml控件配置属性 andro ...

  5. random随机模块,time时间模块

    random /随机模块: 作用: 在某个范围内取到每一个值得概率是相通的. 一.随机小数 random.random() import random print(random.random())  ...

  6. jedisLock—redis分布式锁实现(转)

    一.使用分布式锁要满足的几个条件: 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体 ...

  7. xlrd python excel

     xlrd python excel

  8. mysql语句, 空的 order by , 空的 where

    SQL语句里, 空的 where, where 1 AND status=1 空的 order by, order by null, updatetime desc 这种空值的情况, 是很有用处的: ...

  9. [ACM] FZU 2087 统计数边 (有多少边至少存在一个最小生成树里面)

    Problem Description 在图论中,树:随意两个顶点间有且仅仅有一条路径的图. 生成树:包括了图中全部顶点的一种树. 最小生成树:对于连通的带权图(连通网)G,其生成树也是带权的. 生成 ...

  10. Android Screen Orientation

    Ref:Android横竖屏切换小结 Ref:Android游戏开发之横竖屏的切换(二十七)