【LeetCode】258. Add Digits
题目:
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?
提示:
此题的原理为“九余数定理”,即给定一个非负整数,一个数的数根(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的更多相关文章
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode】 258. Add Digits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...
- 【一天一道LeetCode】#258. Add Digits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 【leetcode】1291. Sequential Digits
题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...
- 【LeetCode】415. Add Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...
- 【LeetCode】002 Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
随机推荐
- [刷题]算法竞赛入门经典(第2版) 5-14/UVa1598 - Exchange
题意:模拟买卖,当出售价bid等于或低于出售价ask,则交易. 代码:(Accepted,0.330s) //UVa1598 - Exchange //Accepted 0.330s //#defin ...
- 使用crontab,让linux定时执行shell脚本
阅读目录 1. cron服务[Ubuntu环境] 2. crontab用法 3. 编辑crontab文件 4. 流程举例 5. 几个例子 Linux中,周期执行的任务一般由cron这个守护进程来处理. ...
- SAP ECC EHP7 RFC 发布成WebService
1.说明介绍 本文将RFC发布成WebService的详细步骤,参考了百度经验http://jingyan.baidu.com/article/8275fc867c9e2946a13cf66c.htm ...
- DDD理论学习系列(3)-- 限界上下文
1. 引言 限界上下文可以拆分为两个词,限界和上下文. 限界:是指一个界限,具体的某一个范围. 上下文:个人理解就是语境. 比如我们常说的段子: "我想静静." 这个句子一般是想表 ...
- JVM、GC与HashMap
阿里巴巴突然来了个面试邀请电话,问了些java底层的东西,不知所措,所以专门花了些时间做了下学习,顺便记录下,好记性不如烂笔头. 一.对JAVA的垃圾回收机制(GC)的理解 不同于C/C++需要手工释 ...
- 对RabbitMQ.Client进行一下小小的包装,绝对实用方便
RabbitMQ是一个老牌的非微软的消息队列组件,一般来说应该能满足中小型公司对消息队列生产的需求,平时我们在.NET开发环境下运用它是可能会需要RabbitMQ.Client的SDK库,此库是官网提 ...
- 深度解析PHP数组函数array_merge
很久之前就用到过这个函数,只不不过是简单的用用而已并没有做太深入的研究 今天在翻阅别人博客时看到了对array_merge的一些使用心得,故此自己来进行一次总结. array_merge是将一个或者多 ...
- Android WebView 不支持 H5 input type="file" 解决方法
最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了H5并嵌入webview中.发现点击H5中 标签 不能打开android资源管理器. 通过网络搜索发现是因为 android webvie ...
- Java之枚举----小试牛刀练习
1.定义一个电脑品牌枚举类,其中只有固定的几个电脑品牌. 1.1简单枚举类,不设置属性和方法 package 第十四章枚举; public enum Brand { Lenovo,Dell,Accer ...
- Python成长之路 — 字典
一.字典的定义与创建 字典是Python中唯一内建的映射类型.你可以将其想象成书本的目录,章节名称代表"key",页码则代表"value".书本的目录本质上是也 ...