题目:

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?

提示:

此题的原理为“九余数定理”,即给定一个非负整数,一个数的数根(Digit Root)与它和小于它的最大的九的倍数有关。举例来说,11的数根是2,因为9+2=11。2025的数根是1,因为(2035 - 1) % 9 = 0。

基于这个定理,可以总结出求一个非负整数的数根公式如下:

代码:

class Solution {
public:
int addDigits(int num) {
return num - * ((num - ) / );
}
};

参考:

https://en.wikipedia.org/wiki/Digital_root

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

  1. 【LeetCode】258. Add Digits (2 solutions)

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

  2. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  3. 【一天一道LeetCode】#258. Add Digits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  5. 【leetcode】1291. Sequential Digits

    题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...

  6. 【LeetCode】415. Add Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  7. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

  8. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  9. 【LeetCode】2.Add Two Numbers 链表数相加

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

随机推荐

  1. Guvav:Options使用和避免NULL

    JAVA中的null null是Java中一个很重要的概念.null设计初衷是为了表示一些不确定的东西.null是Java中的关键字,它是大小写敏感的,你不能将null写成Null或NULL,编译器将 ...

  2. Tensorflow中的padding操作

    转载请注明出处:http://www.cnblogs.com/willnote/p/6746668.html 图示说明 用一个3x3的网格在一个28x28的图像上做切片并移动 移动到边缘上的时候,如果 ...

  3. 用SIM900上传云端数据(原创KEKE)

    依次发送如下几个命令到SIM900A模块:   AT+CGCLASS="B" AT+CGDCONT=1,"IP","CMNET" AT+CG ...

  4. tab切换插件开发

    我开发的tab切换插件,基于jquery库,实现tab标签页的切换.插件的名称为jquery.tabSwitch.js. 插件实现代码如下: ; (function ($) { $.fn.tabSwi ...

  5. mysql远程授权

    切换到mysql的mysql数据库,找到user表: cmd:mysql -u root -p cmd:use mysql; cmd:select host,user,password from us ...

  6. React 读书笔记

    序言: 领导安排部门同事本月内看一本跟自己职业相关的书籍, 根基类的书籍已经看过了,重复阅读的意义不大,所以我平时看的都是视频,也许是视频作者没有出书的条件,也许是现在出书看的人越来越少了,也许有其他 ...

  7. 常见类——Object

    1.在Java类继承结构中Java.lang.Object类位于顶端   2.如果定义一个Object类没有使用extends关键字声明其父类,则其父类为Java.lang.Object类   3.O ...

  8. node.js入门系列(一)--Node.js简介

    什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器. 每一种解析器都是一 ...

  9. 信号处理——卷积(convolution)的实现

    作者:桂. 时间:2017-03-07  22:33:37 链接:http://www.cnblogs.com/xingshansi/p/6517301.html 前言 信号时域.频域对应关系,及其D ...

  10. Unity3D 骨骼动画原理学习笔记

    最近研究了一下游戏中模型的骨骼动画的原理,做一个学习笔记,便于大家共同学习探讨. ps:最近改bug改的要死要活,博客写的吭哧吭哧的~ 首先列出学习参考的前人的文章,本文较多的参考了其中的表述: 1. ...