题目:

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.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

代码:

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

思路:

The problem, widely known as digit root problem, has a congruence formula:

https://en.wikipedia.org/wiki/Digital_root#Congruence_formula
For base b (decimal case b = 10), the digit root of an integer is: dr(n) = 0 if n == 0
dr(n) = (b-1) if n != 0 and n % (b-1) == 0
dr(n) = n mod (b-1) if n % (b-1) != 0
or dr(n) = 1 + (n - 1) % 9
Note here, when n = 0, since (n - 1) % 9 = -1, the return value is zero (correct). From the formula, we can find that the result of this problem is immanently periodic, with period (b-1). Output sequence for decimals (b = 10): ~input: 0 1 2 3 4 ...
output: 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 .... Henceforth, we can write the following code, whose time and space complexities are both O(1). class Solution {
public:
int addDigits(int num) {
return 1 + (num - 1) % 9;
}
};

LeetCode: 258 Add Digits(easy)的更多相关文章

  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. (easy)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(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定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. Java [Leetcode 258]Add Digits

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

  7. LeetCode 258 Add Digits 解题报告

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

  8. leetcode 258. Add Digits(数论)

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

  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. SVG 签名动画 制作

    不知道哪天看到的一个朋友圈里面有发的什么什么免费教签名之类的,就看了下SVG,做这功能还不错. 主要用到的几个属性,需要自行百度一下,不详说  stroke-dashoffset ,  stroke- ...

  2. opencv中的SVM图像分类(二)

    opencv中的SVM图像分类(二) 标签: svm图像 2015-07-30 08:45 8296人阅读 评论(35) 收藏 举报  分类: [opencv应用](5)  版权声明:本文为博主原创文 ...

  3. iOS之简单瀑布流的实现

    iOS之简单瀑布流的实现   前言 超简单的瀑布流实现,这里说一下笔者的思路,详细代码在这里. 实现思路 collectionView能实现各中吊炸天的布局,其精髓就在于UICollectionVie ...

  4. 关于移动端文字无法垂直居中(或line-height不起作用)的问题的解决方案(网摘)

    最近开发移动端APP,发现安卓端对于文字垂直居中的问题兼容性不好,网上也搜了很多方法,但是都比较麻烦,自己摸索出来了最终的解决方案: 1.首先在html头部把我们常用的lang="en&qu ...

  5. 理解DOMSTRING、DOCUMENT、FORMDATA、BLOB、FILE、ARRAYBUFFER数据类型

    一.XMLHttpRequest 2.0的家臣们 我大学那会儿,一个称为Ajax的东西对前端行业造成了深远影响,不仅是JS语言,而包括前端地位.职位兴起以及工作分工等.抛开IE6浏览器不谈,其他浏览器 ...

  6. 【TensorFlow-windows】(零)TensorFlow的"安装"

    Tensorflow的安装,具体操作就不演示了.具体操作请移步: http://blog.csdn.net/darlingwood2013/article/details/60322258#comme ...

  7. 目标检测之积分图---integral image 积分图2

    前面在图像处理一栏中涉及到boxfilter 的时候,简单介绍过积分图,就是每个像素点是左边和上边的累加和,这样的话可以方便均值和方差,以及直方图统计的相关运算,这里再次结合网络资源重新单独对积分图做 ...

  8. 果壳、推库、虎秀、知乎、it世界

    果壳.推库.虎秀.知乎.it世界

  9. DotNetBar MessageBoxEx 显示中文 显示office2007风格

    MessageBoxEx显示消息的时候按钮是中文的解决这个问题设置 MessageBoxEx的UseSystemLocalizedString属性为 true. MessageBoxEx.UseSys ...

  10. [iOS] 初探 iOS8 中的 Size Class

    本文转载至  http://www.itnose.net/detail/6112176.html   以前和安卓的同学聊天的时候,谈到适配一直是一个非常开心的话题,看到他们被各种屏幕适配折磨的欲仙欲死 ...