[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
.
Since2
has 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 ...
随机推荐
- 【转】java:Session(会话)机制详解
书中讲:以下情况,Session结束生命周期,Servlet容器将Session所占资源释放:1.客户端关闭浏览器2.Session过期3.服务器端调用了HttpSession的invalidate( ...
- 深入理解 Neutron -- OpenStack 网络实现(3):VXLAN 模式
问题导读1.VXLAN 模式下,网络的架构跟 GRE 模式类似,他们的不同点在什么地方?2.网络节点的作用是什么?3.tap-xxx.qr-xxx是指什么? 接上篇:深入理解 Neutron -- O ...
- ThinkPad L421 如何进入BIOS?(已解决)
开机屏幕出现ThinkPad标志时,快速按下 F1键 即可进入BIOS
- Python Tkinter Text控件
原文地址: http://blog.csdn.net/bemorequiet/article/details/54743889 这篇博客主要是简单的说一下Tkinter中的Text控件的相关知识. T ...
- How to install Wine on Ubuntu Linux 64bit
参考地址:https://linuxconfig.org/how-to-install-wine-on-ubuntu-linux-64bit The following linux command p ...
- 总结一下最近用到的技术(1)--ultraESB
最近项目中方法用到了一些新的东西,由于之前没有用到过,现在总结一下,方便以后查阅,本篇文章介绍ultraESB,接下来的文章会介绍JsonSchema,JsonSchamaValidator,Json ...
- Unity3D 面试三 ABCDE
说说AB两次面试: “金三银四” 三月份末又面试过两家:共和新路2989弄1号1001这家找了我半天,哇好漂亮的办公大楼!问了保安才知道,这个地址是小区地址.另一家也是创业公司面试我的自称是在腾讯做过 ...
- Unity3D 记第二次面试
2014-03-10 忍不住投递了几份简历大概有20个,总共收到面试电话2个,十分之一.一个是11号下午4点面试另一个是12号下午3点面试(后来没去至于原因下面有)12号没去,为什么?因为招聘要求“精 ...
- 解析xml文件的几种技术与Dom4j与sax之间的对比
一.解析xml文件的几种技术:dom4j.sax.jaxb.jdom.dom 1.dom4j dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常优秀的 ...
- 无约束优化方法(梯度法-牛顿法-BFGS- L-BFGS)
本文讲解的是无约束优化中几个常见的基于梯度的方法,主要有梯度下降与牛顿方法.BFGS 与 L-BFGS 算法. 梯度下降法是基于目标函数梯度的,算法的收敛速度是线性的,并且当问题是病态时或者问题规模较 ...