[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 ...
随机推荐
- ( ( (int(*)(uint, ushort, uint *, uint, int)) (*((uint *)(TCM_BASE + 0x8))) ) (a,b,c,d,e) )
( ( (int(*)(uint, ushort, uint *, uint, int)) (*((uint *)(TCM_BASE + 0x8))) ) (a,b,c,d,e) ) 首先红 ...
- List的五种去重方式
//set集合去重,不改变原有的顺序 public static void pastLeep1(List<String> list){ System.out.println("l ...
- POP3命令与分析
http://www.cnblogs.com/crystalray/p/3302121.html POP3(Post Office Protocol 3)即邮局协议的第3个版本,它是规定个人计算机如何 ...
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
- 压力测试工具JMeter入门教程<转>
1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...
- CentOS 安装Passenger
gem install passenger 查看路径 passenger-config --root passenger-install-apache2-module ps auxw | grep f ...
- 【CF772D】Varying Kibibits FWT
[CF772D]Varying Kibibits 题意:定义函数f(a,b,c...)表示将a,b,c..的10进制下的每一位拆开,分别取最小值组成的数.如f(123,321)=121,f(530, ...
- 初始react
很久就期待学习react了,惰性,一直都没有去翻阅react的资料,最近抽空,简单的了解了一下react,先记录一下,后续慢慢的学习. 一.ReactJS简介 React 起源于 Facebook 的 ...
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- 8.22js前端!
2018-8-22 19:15:19 前端还有一部分就学完了预计这一星期赶一下就可以结束 然后愉快地进行Django框架!!!! 今天中午和呆呆一块吃的大盘鸡,下午和老表我们三个去了日月湖狼了一下午! ...