Problem:

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?

此题最初想法是相加、判断反复循环直到找到满足的解。通过参考别人的解答发现,此题是让我们求“数根”。

由此解法一为通过循环相加判断,直到找到可行解:

class Solution
{
public:
int addDigits(int num)
{
int add = ;
while (num >= )
{ while(num > )
{
add += num % ;
num /= ;
}
num = add;
}
return num;
}
};

但是由于时间复杂度超过题目要求,故考虑针对“数根”的解法。

通过枚举可知:   
10 1+0 = 1
11 1+1 = 2
12 1+2 = 3    
13 1+3 = 4
14 1+4 = 5
15 1+5 = 6
16 1+6 = 7
17 1+7 = 8
18 1+8 = 9
19 1+9 = 1
20 2+0 = 2

于是可得出规律,每9个一循环。为处理9的倍数的情况,我们写为(n - 1) % 9 + 1。由此可得:

class Solution
{
public:
int addDigits(int num)
{
return (num - ) % + ;
}
};

LeetCode 258. Add Digits的更多相关文章

  1. LN : leetcode 258 Add Digits

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

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

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

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

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

  4. (easy)LeetCode 258.Add Digits

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

  5. Java [Leetcode 258]Add Digits

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

  6. LeetCode 258 Add Digits 解题报告

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

  7. leetcode 258. Add Digits(数论)

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

  8. LeetCode: 258 Add Digits(easy)

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

  9. leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇

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

随机推荐

  1. 3D游戏编程大师技巧──环境搭建

    刚开微博,想借助这个平台与大家交流,写下自己的学习记录,希望得到大家的批评指正. 好了,进入主题.这段时间对游戏编程很感兴趣,于是在网友的推荐下开始学习<3D游戏编程大师技巧>这本书.今天 ...

  2. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  3. Form Builder的三种查询方法构建

    1.使用DEFAULT_WHERE: DECLARE   V_DEFAULT_WHERE VARCHAR2(32767);  V_WHERE         VARCHAR2(32767); BEGI ...

  4. Linux时间同步

    (1)进入 root权限 su root(2)使用ntpdate IP(时间源IP)或service crond restart查看服务有没有开启(3)#crontab -e(新增一下语句) 0 */ ...

  5. Burp Suite 使用教程(上传突破利器)

    Burp Suite是一个免费的网站攻击工具. 它包括proxy.spider.intruder.repeater四项功能.该程序使用Java写成,需要 JRE 1.4 以上版本 下载该程序的源代码, ...

  6. 我的LaTeX中文文档模板

    中文LaTeX处理模板 环境MiTex内核 编辑环境WinEdit 源码如下: \documentclass[a4paper,12pt]{article} \usepackage{CJK} %设定字号 ...

  7. RBAC中 permission , role, rule 的理解

    Role Based Access Control (RBAC)——基于角色的权限控制 permission e.g. creating posts, updating posts role A ro ...

  8. linux文件对比命令——diff

    diff用于比较文件或目录内容,特别是比较两个版本不同的文件以找到改动的地方. 如果指定比较的是文件,则只有当输入为文本文件时才有效,以逐行的方式,比较文本文件的异同处. 如果指定比较的是目录的的时候 ...

  9. [正则表达式]PCRE环视功能

    设想一下这个问题,假设为了方便长串数字的阅读性,需要为其添加逗号作为分隔,需要怎么做呢? 2569836495 => 2,569,836,495 正则表达式的匹配通常是从左往右的,这导致无法使用 ...

  10. tp5 model 的数据自动完成

    auto属性自动完成包含新增和更新操作 namespace app\index\model; use think\Model; class User extends Model { protected ...