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. ...
随机推荐
- oracle入门(3)——oracle服务解释
[本文介绍] oracle不像mysql,安装后之后一个服务,如果mysql连接不上,打开其服务就行.oracle是有多个服务,哪些服务要开,哪些服务没必要开,对新手来说未必不是一个难点.下面对ora ...
- 使用Xcode改动iOS项目project名和路径名
对,好.错.改正. ------ 前言 系统 10.9 开发平台 xcode 5.0 旧project名 MyProject-iPad 改动之后 新project名 FjSk-iPad 点击项目,进入 ...
- PyNest——part 3: connecting networks with synapses
part 3: connecting networks with synapses parameterising synapse models NEST提供了各种不同的突触模型. 您可以使用命令nes ...
- SQL LEFT JOIN
SQL LEFT JOIN 关键字 SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有 ...
- linux进程内存到底怎么看 剖析top命令显示的VIRT RES SHR值
引 言: top命令作为Linux下最常用的性能分析工具之一,可以监控.收集进程的CPU.IO.内存使用情况.比如我们可以通过top命令获得一个进程使用了多少虚拟内存(VIRT).物理内存(RES). ...
- Hbase 学习笔记2----概念
说在前面,本文部分内容来源于社区官网经过适度翻译,部分根据经验总结,部分是抄袭网络博文,(不一一列举引用,在此致歉)一并列在一起,本文的目的,希望能总结出一些有用的,应该注意到的东西,基本思路是先提出 ...
- Authentication Overview
Authentication Overview Service accounts User accounts API keys
- Java集合(7):HashMap
一.HashMap介绍 HashMap是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据hash算法来来计算 ...
- Java基础知识陷阱(一)
本文发表于本人博客. 事隔好多年了,重新拿起来Java这门语言,看似熟悉其实还很陌生,想想应该梳理下顺便提高下自己.这次先来看看Java里面的String这个对象. 经典的先看下面一段代码,请问最终创 ...
- 60. Permutation Sequence(求全排列的第k个排列)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...