LeetCode 258 Add Digits(数字相加,数字根)
翻译
给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数。
比如:
给定sum = 38,这个过程就像是:3 + 8 = 11。1 + 1 = 2。由于2仅仅有一位数。所以返回它。
紧接着:
你能够不用循环或递归在O(1)时间内完毕它吗?
原文
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?
分析
事实上我并不会写,循环和递归都不能用……我还能怎么办呐。
然后看了LeetCode上给的提示。看了维基百科的一篇文章:Digital root。
有了这个的话,就非常easy了。详细这个公式的细节,大家自己參看维基吧,由于篇幅过长就不翻译了。
代码
class Solution {
public:
int floor(int x) {
return (x - 1) / 9;
}
int addDigits(int num) {
return num - 9 * floor(num);
}
};
LeetCode 258 Add Digits(数字相加,数字根)的更多相关文章
- 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 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- 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 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- leetcode 258. Add Digits(数论)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 258. Add Digits 数位相加到只剩一位数
[抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode: 258 Add Digits(easy)
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
随机推荐
- less13 颜色值函数
//通过十进制红色,绿色,蓝色三种值 (RGB) 创建不透明的颜色对象. div{ background: rgb(255,0,0); background: rgb(100%,0%,0%); } / ...
- Java多线程理解:线程安全的集合对象
1.概念介绍 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用.不会出现数据不一致或者数据污染. 线程不安全就 ...
- BZOJ 3223 Splay区间翻转
思路: 区间翻转的裸题 终于tm理解splay了-- //By SiriusRen #include <cstdio> #include <cstring> #include ...
- 排序算法(Apex 语言)
/* Code function : 冒泡排序算法 冒泡排序的优点:每进行一趟排序,就会少比较一次,因为每进行一趟排序都会找出一个较大值 时间复杂度:O(n*n) 空间复杂度:1 */ List< ...
- UI Framework-1: Ash Color Chooser
Ash Color Chooser Overview This document describes how to achieve <input type=”color”> UI in C ...
- Mac配置PHP环境
本文章来自:http://blog.csdn.net/wj_november/article/details/51417491 本人使用的是:MacOs 10.12.3,根据如上操作已经安装成功,感谢 ...
- [Bug]Python3.x AttributeError: libtest.so: undefined symbol: fact
写kNN,需要在python中实现kd-tree 思考了一下,在python下写这种算法类的东西,还是十分别扭 于是希望用ctypes调用一下c++动态加载库 于是尝试实现一下 // test.cpp ...
- Python组织文件 实践:将文件的不同版本备份为ZIP文件
功能:备份文件夹.能将文件的不同版本备份下来,并且每个有不同的名字 #! python3 # backupToZip.py - 备份文件的不同版本到压缩文件中 import zipfile,os #f ...
- 【Codeforces Round #462 (Div. 1) A】 A Twisty Movement
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] ans初值值为a[1..n]中1的个数. 接下来考虑以2为结尾的最长上升子序列的个数. 枚举中间点i. 计算1..i-1中1的个数c ...
- POJ 3670 Eating Together(LIS)
Description The cows are so very silly about their dinner partners. They have organized themselves i ...