翻译

给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数。

比如:

给定sum = 38,这个过程就像是:3 + 8 = 11。1 + 1 = 2。由于2仅仅有一位数。所以返回它。

紧接着:

你能够不用循环或递归在O(1)时间内完毕它吗?

原文

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?

分析

事实上我并不会写,循环和递归都不能用……我还能怎么办呐。

然后看了LeetCode上给的提示。看了维基百科的一篇文章:Digital root。

有了这个的话,就非常easy了。详细这个公式的细节,大家自己參看维基吧,由于篇幅过长就不翻译了。

代码

class Solution {
public:
int floor(int x) {
return (x - 1) / 9;
}
int addDigits(int num) {
return num - 9 * floor(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 加数字

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

  3. LeetCode 258. Add Digits

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

  4. Java [Leetcode 258]Add Digits

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

  5. LeetCode 258 Add Digits 解题报告

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

  6. leetcode 258. Add Digits(数论)

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

  7. (easy)LeetCode 258.Add Digits

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

  8. 258. Add Digits 数位相加到只剩一位数

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

  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. IE11 补丁 KB2929437[已过期]

    2014年4月 请更新此补丁 KB2929437 开发人员工具有重要更新 DOM 面板右侧新增 "更改"面板,用于记录调试时修改的 CSS Rules: JS 调试面板,新增 so ...

  2. 1570. [POJ3461]乌力波

    ★☆   输入文件:oulipo.in   输出文件:oulipo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 法国作家乔治·佩雷克(Georges Perec,1 ...

  3. Android-加载大图避免OOM

    高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...

  4. The view 'Index' or its master was not found or no view engine supports the

    ASP.net  MVC 5  WebApi部署IIS提示: 未找到视图“索引”或其母版视图,或没有视图引擎支持搜索的位置.搜索了以下位置: 其他设置一切正常 这种情况很有可能是,1.部署的路径中空格 ...

  5. Linux Putty 复制粘贴

    从putty复制:    用左键选中文字,再其他地方点中键就可以粘贴了,不需要右键粘贴. 在putty中粘贴:    在其他地方用左键选中文字,不用右键复制,保持选中文字高亮,然后在putty中点中键 ...

  6. c3p0-config.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <c3p0-confi ...

  7. celery 学习

    1. 列出计划的ETA任务(worker) celery -A proj inspect scheduled 参考文档:http://docs.celeryproject.org/en/latest/ ...

  8. Python 批处理文本文件、进行查找

    去年换了一部手机,老手机终于光荣退休了,但是里面的便签里还存有很多文字记录,这个手机还不能备份到云,只能将每个便签保留为一个个的文本文件,我想要把所有的文本文件归到一个文本文件中,手动操作太麻烦了,刚 ...

  9. 题解 CF1000E 【We Need More Bosses】

    这道题绝不是紫题... 题目的意思其实是让你求一个无向无重边图的直径. 对于求直径的问题我们以前研究过树的直径,可以两遍dfs或者两边bfs解决. 对于图显然不能这样解决,因为图上两点之间的简单路径不 ...

  10. 使用Java8提供的Duration类制作字幕时间轴调整工具

    网上下载的字幕有时和片源的时间轴不一致.我们能够自己写一个工具来调整,也就是总体向前移动几秒,或者向后移动几秒.Java8中提供的Duration类使得这样的时间计算极其方便.以下就以最简单的srt字 ...