【LeetCode】Add Digits
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?
Solution:
考虑 O(1) 的算法,从最朴素的数字开始:
0 - 9 无疑都对应返回 0 - 9 就行了。
接下来,
10 返回 1
11 返回 2
12 返回 3
13 返回 4
……
18 返回 9
19 返回 1
……
规律呼之欲出,因为最终所有的数字都将被 映射到 0 - 9 这 10 个数字上,所以才开始考虑这样的映射是否存在一些本质上的规律,那么由上述可以发现规律就是随着数字本身的大小递增,最后映射到的数其实也是递增的,但这样的递增其实是以 9 为模,而对于此题我们最后只关心取模后的余数而不关心数字本身有几个“模9”了。
需要注意的一点是:
0 在这里是特殊的,只有 0 数字本身映射到 0 ,再也没有其他数字映射到 0,需要单独处理。
所以实际上 我们是将 除0外的数字 映射 到 1-9 这九个数字上,想想便知结果应该是 (num-1) % 9 + 1。
代码如下:
class Solution:
# @param {integer} num
# @return {integer}
def addDigits(self, num):
if num == 0:
return 0
else:
return (num-1) % 9 + 1
【LeetCode】Add Digits的更多相关文章
- 【02_258】Add Digits
		Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ... 
- 【leetcode】Add Two Numbers
		题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ... 
- 【题解】【链表】【Leetcode】Add Two Numbers
		You are given two linked lists representing two non-negative numbers. The digits are stored in rever ... 
- 【leetcode】Add Two Numbers(middle) ☆
		You are given two linked lists representing two non-negative numbers. The digits are stored in rever ... 
- 【leetcode】 Add Two Numbers
		You are given two linked lists representing two non-negative numbers. The digits are stored in rever ... 
- 【LeetCode】Add Two Numbers(两数相加)
		这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ... 
- 【LeetCode】Reverse digits of an integer
		Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ... 
- 【leetcode】Add Binary
		题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ... 
- 【LeetCode】数学(共106题)
		[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ... 
随机推荐
- enable feature AJAX of MOSS2007
			As default, the feature AJAX of MOSS2007 is disabled, so the site web configuration file should be m ... 
- Python:dict用法
			dict全称dictionary,使用键-值(key-value)存储,有极快的查找速度. 以下整理几种常用的dict用法 定义 空dict >>> dict={} 普通dict & ... 
- Sublime Text 2 配置(转载)
			转载 自 Sublime Text 2 的详细配置(C++) 想起暑假在公司偷偷写题,用不惯vs ,配sublime 又一直编译不了...每次用codeblocks 眼泪掉下来www 下载sublim ... 
- F2工作流引擎Web层全新扁平化UI上线
			特点:引入Bootstrap开源UI样式和fontawesome图标集 扁平化样式使用界面更舒服,按钮主题可快速定义更换,对于集成到业主系统UI图标更加丰富. 以下截取部分图片展示,更多请联系作者登录 ... 
- QT  做软件盘
			最近搞了一个组织细胞脱水机项目,当然,对于国内的项目都是仿来仿去的,我们也不例外,开启被仿机器后,第一个看到的界面就是用户登录界面,需要输入中文,作为一个程序员,我的第一反应就是我需要采用什么用的框架 ... 
- JAVA  循环语句的练习
			/*for(int i=1;i<=10;i++) //输出一个三角形 { for (int j=1;j<=i;j++) { System.out.print("*" ... 
- Python的50个模块,满足你各种需要
			Python具有强大的扩展能力,我列出了50个很棒的Python模块,包含几乎所有的需要:比如Databases,GUIs,Images, Sound, OS interaction, Web,以及其 ... 
- Ubuntu日常问题搜集和解决办法
			搜集了日常工作中linuxmint的使用的命令备份和遇到的问题以及解决办法.(持续更新中) 保持ssh链接超时不自动断开 用ssh链接服务端,一段时间不操作或屏幕没输出(比如复制文件)的时候,会自动断 ... 
- JS中函数的调用和this的值
			调用每一个函数会暂停当前函数的执行,传递控制权和参数给新函数.除了声明时定义的形式参数,每个函数还接收两个附加的参数:this 和 arguments. 参数this在面向对象编程中非常重要,他的值取 ... 
- UVA 11297 线段树套线段树(二维线段树)
			题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ... 
