题目:

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. android IPC通信(上)-sharedUserId&&Messenger

    看了一本书,上面有一章解说了IPC(Inter-Process Communication,进程间通信)通信.决定结合曾经的一篇博客android 两个应用之间的通信与调用和自己的理解来好好整理总结一 ...

  2. JavaScript读书笔记(2)--数据类型

    1.  严格模式:在javascript中定义了一种不同的解析与执行模型.在严格模式下,一些不确定的行为将得到处理,对某些不安全的操作也会抛出错误. 用法是在脚本中添加:”use strict”; 这 ...

  3. Navicat Premium创建MySQL存储过程

    1.使用Navicat Premium打开创建函数向导,操作:连接名——数据库——函数——新建函数 2.选择过程——输入存储过程参数——完成(这一步可以不填写参数,编写存储过程代码的时候设置参数) 3 ...

  4. 通过WindowManager图片切换的效果

    最近为这个事情焦头烂额,原因无他.原来打算是把ViewPager放在WindowManager中,再设定一个定时器,让图片自动切换,但是搞了很久,发现无论如何,这个图片只显示一张.虽然日志看得出来图片 ...

  5. Learning Scrapy 中文版翻译 第二章

    为了从网页中提取信息,你有必要对网页的结构做一些了解.我们将快速学习HMTL,HTML数状结构以及用XPath在网页上提取信息 HTML, DOM树结构以及XPath 让我们花一点时间来了解当用户在浏 ...

  6. 设置port转发来訪问Virtualbox里linux中的站点

    上一篇中我们讲到怎么设置virtuabox来通过SSH登录机器. 相同.我们也能够依照上一篇内容中的介绍,设置port转发,来訪问虚拟linux系统已经搭建的站点: 1.设置port转发: water ...

  7. git branch 分支创建时间排序

    git branch日期排序 vi ~/.gitconfig [alias]lb = !"for k in `git branch -a|perl -pe s/^..//`;do echo ...

  8. 事件 MotionEvent

    点击和长按可能会同时发生,需要在长按的回调函数中返回true,就不会产生点击.谁处理事件谁就是消费者 如果view组件不处理事件,最后会让ontouchevent处理,它是备胎 <LinearL ...

  9. Python类的特殊属性

    Python中的特殊属性 定义如下类: class Foo(object): """Foo class definition""" 类的特殊 ...

  10. Windows程序设计(0)——编程之前

    Windows程序设计之前 1 做什么 2 解决什么问题 3 有哪些资源 在开始真正的编程之前,需要了解要做的事情是什么,要解决的解决的问题是什么,有哪些资源可以使用. 1 Windows程序设计之前 ...