[LeetCode] 258. Add Digits_Easy tag: Math
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Example:
Input:38
Output: 2
Explanation: The process is like:3 + 8 = 11,1 + 1 = 2.
Since2has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
基本方法, 用一个helper function, 去计算num的digits sum, 然后如果不是< 10, 继续循环call helper function.
Math 方法: O(1)
The problem, widely known as digit root problem, has a congruence formula:
https://en.wikipedia.org/wiki/Digital_root#Congruence_formula
For base b (decimal case b = 10), the digit root of an integer is:
- dr(n) = 0 if n == 0
- dr(n) = (b-1) if n != 0 and n % (b-1) == 0
- dr(n) = n mod (b-1) if n % (b-1) != 0
or
- dr(n) = 1 + (n - 1) % 9
Note here, when n = 0, since (n - 1) % 9 = -1, the return value is zero (correct).
Code
1) while
class Solution:
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
def helper(num):
return sum(int(c) for c in str(num))
while num > :
num = helper(num)
return num
2) Math
class Solution:
def addDigits(self, num):
if num == 0: return 0
return 1 + (num-1)%9
[LeetCode] 258. Add Digits_Easy tag: Math的更多相关文章
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 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 616. Add Bold Tag in String
原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...
- 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. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- (easy)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 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
随机推荐
- Git学习之Git恢复进度
================================================ 继续暂存区未完成的实践 ======================================= ...
- namespace main
c++程序运行的入口是::main 如果把main放到某个命名空间中,则系统就无法找到入口. 所以就有了为了测试功能的tools和test,其中没有命名空间.
- office excel Query 功能
Microsoft Query 合并查询 Excel 2016 默认已集成 Power Query 功能
- 【CF839E】Mother of Dragons 折半状压
[CF839E]Mother of Dragons 题意:给你一张n个点,m条边的无向图.你有k点能量,你可以把能量分配到任意一些点上,每个点分到的能量可以是一个非负实数.定义总能量为:对于所有边&l ...
- [工具] 各种主流 SQLServer 迁移到 MySQL 工具对比
我之所以会写这篇对比文章,是因为公司新产品研发真实经历过这个痛苦过程(传统基于SQL Server开发的C/S产品转为MySQL云产品). 首次需要数据转换是测试环节,当时为了快速验证新研发云产品性能 ...
- 自动释放池autoreleasepool
自动释放池是NSAutoreleasePool的实例,其中包含了收到autorelease消息的对象.当一个自动释放池自身被销毁(dealloc)时,它会给池中每一个对象发送一个release消息(如 ...
- 服务器群秒级别文件同步(ssh+SHELL)
1.介绍 \ 2.业务服务器远程更新浏览服务器文件的脚本 #!/bin/bash operate=$ ip=$ conf_file="/var/www/html/test/ip_list&q ...
- Android开发中Chronometer的用法
Chronometer集成自TextView,里面有个Handler负责定时更新ui. 其计时原理很简单:通过setBase(long t)方法设置好baseTime之后,当start()时,每隔一秒 ...
- 10W年薪和30W+年薪的产品经理差距在哪?
举办到今年第六届壹佰案例峰会,认识的“程序猿/媛”朋友越来越多,时间长了就发现,程序员的世界一点也不单调,外界传说的不善言辞.最大的乐趣就是买“机械键盘”都是不对的.你见过周末组团去山里骑哈雷的研发经 ...
- 肖俊:HPE IT 的DevOps 实践分享
本篇文章来自于HPE和msup共同举办的技术开放日HPE测试技术总监肖俊的分享,由壹佰案例整理编辑. 一.DevOps含义解析 这是DevOps的趋势图.DevOps这个概念大概是在2009年被提出来 ...