题目:

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. MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)

    搭建SpringMVC的-->传送门<-- 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话,pom.xml的配置如下: <?xml version=" ...

  2. 软考考点---CPU

    软考考点---CPU         中央处理器(CPU,Central Processing Unit)是一块超大规模的集成电路,是一台计算机的运算核心(Core)和控制核心( Control Un ...

  3. sanic官方文档解析之Example(二)

    1,通过docker部署sanic项目 通过Docker和Docker Compose部署SANIC应用程序是一项很容易实现的任务,下面的示例提供了示例simple_server.py的部署 FROM ...

  4. 九度OJ 1141:Financial Management (财务管理) (平均数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...

  5. How to deploy a Delphi OSX project from the command line

    Delphi has a well developed command line build process (via MSBuild) for Windows projects. After the ...

  6. 20170313 ABAP程序未激活状态下保存或激活进入debug

    自动进入断点,没有设置的.FUNCTION RS_NEW_PROGRAM_INDEX. https://archive.sap.com/discussions/message/14132983 解决办 ...

  7. office web apps的搭建部署(1)(写于2017.12.27)

    因为业务方面的需求,项目要求搭建office-web-apps这个玩意儿,做一个在线预览编辑的功能,为了方便,我下面都用OWA代替这个服务. 首先说一下什么是office-web-apps-serve ...

  8. javascript中获取class

    js中没有获取class的办法,找了一些封装好的方法,这里整理一下 (1)先进行封装 //封装getClass function getClass(tagName,className) //获得标签名 ...

  9. loj#2340. 「WC2018」州区划分

    FWT&&FMT板子 #include<cstdio> #include<iostream> #include<cstring> #include& ...

  10. 关于Ajax实现的简单示例

    一.代码示例 关于Ajax的基本概念(包括XMLHttpRequest对象及其相关方法属性)移步这里(w3school中文版)学习了解. <!doctype html> <html ...