LeetCode OJ: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?
想要做出来很简单,但是关键在于这里的最后一句话,要在O(1)时间内,而且不能使用循环或者递归。
这里就要说说有关数字根的概念了:
定义
数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止。而这个一位数便是原来数字的数字根。适用范围为正整数和零。例如:
1的数字根为1
10的数字根为1(1+0=1)
21的数字根为3(2+1=3)
48的数字根为3(4+8=12,1+2=3)
198的数字根为9(1+9+8=18,1+8=9)
性质说明
1.任何数加9的数字根还是它本身。
2.9乘任何数字的数字根都是9。
3.数字根的三则运算
1.两数之和的数字根等于这两个数的数字根的和数字根
2.两数之积的数字根等于这两个数的数字根的积的和数字根
3.一个数字的n次幂的数字根等于这个数字的数字根的n次幂的和数字根
这一部分是转载 数字根介绍 的;
所以可以得知计算数字根的公式就是:root = (num - 1) % 9 + 1; PS:这里的 -1主要是为了避免num正好是9的倍数,那样数根应该正好是9, 而不应该是0才对。
代码不言而喻,只不过道理应该想清楚才对:
考虑一下 result = (num - 1) % 9 + 1; 那么就有
result - 1 = (num - 1) % 9; 那么 digitRoot(result - 1) == digitRoot(num - 1);
那么根据上面所说的,就有:result的数字根与num的数字根就是一样的了。所以这个等式成立。
class Solution {
public:
int addDigits(int num) {
return (num - )% + ;
}
};
java版本代码如下所示:
public class Solution{
public int addDigits(int num){
return (num - 1)%9 + 1;
}
}
LeetCode OJ:Add Digits(数字相加)的更多相关文章
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- LeetCode 258 Add Digits(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- [LeetCode] 258. Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- LeetCode 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
随机推荐
- 批量处理任务进度条控制—基于BackgroundWorker
今天要做一个批量处理图层数据的功能,希望在处理任务过程中,各个任务都能在进度条中显示自己的当前进度,决定继续使用强大易用的BackgroundWorker组件.通过在RunWorkerComplete ...
- Go 结构体和map等数据结构转json字符串
Go语言中使用json包中的 Marshal() 函数将数据结构转成json字符串,源代码: func Marshal(v interface{}) ([]byte, error) { e := ne ...
- CG group
Linux CGroup全称Linux Control Group, 是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如CPU.内存.磁盘输入输出等).这个项目最早是由Google ...
- linux命令行与shell脚本编程 -----15控制脚本
常见的Linux系统信号 信号 值 描述 1 SIGHUP 挂起进程 2 SIGINT 终止进程 3 SIGQUIT 停止进程 9 SIGKILL 无条件终止进程 15 SIGTERM 可能的话终止进 ...
- 浅谈 JS 内存泄露方式与避免方法(二)
Concept WHAT : 内存泄露是指一块被分配的内存既不能使用,又不能回收,直到浏览器进程结束.正常情况下,垃圾回收器在DOM元素和event处理器不被引用或访问的时候回收它们.但是,IE的早些 ...
- Django框架之模板基础,静态文件配置
一.模板继承 目的是:减少代码的冗余 语法: {% block classinfo %} {% endblock %} 具体步骤: 1.创建一个base.html文件,2.把要显示的页面的内容写在这里 ...
- NHibernate应用开发
第一章:NHibernate入门 第一讲:NHibernate架构剖析 第二讲:搭建第一个NHibernate应用程序 第三讲:nhibernate.cfg.xml ...
- rails 单数 复数 大写 小写转换 下划线 驼峰命名
downcase 变小写 pluralize 复数 singularize 单数 camelcase 驼峰 underscore : “MyScore”.undersocre ==> my_s ...
- C语言预处理命令之条件编译(#ifdef,#else,#endif,#if等)
转自:http://www.kuqin.com/language/20090806/66164.html 预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器 ...
- H3C交换机配置镜像端口
配置步骤 进入配置模式:system-view: 创建本地镜像组:mirroring-group 1 local 为镜像组配置源端口:mirroring-group 1 mirroring-port ...