leetcode add digits
来源:互联网 发布:mac迅雷没速度 编辑:程序博客网 时间:2024/06/03 17:56
视频分析:
http://v.youku.com/v_show/id_XMTMzMjUwNjk3Mg==.html
题干:
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?
代码:
from math import *class Solution(object): def sumdig(self,num): re=0 tmp=num while True: re+=tmp%10 tmp=tmp//10 if tmp<=0: break return re def addDigits2(self, num): """ :type num: int :rtype: int """ tmp=num while True: if tmp<10 and tmp>=0: return tmp else: tmp=self.sumdig(tmp) def addDigits(self, num): return int(num-9*floor((num-1)/9))
0 0
- [leetcode][math] Add Digits
- [leetcode] Add Digits
- 【leetcode】Add Digits【java】
- Leetcode: Add Digits
- LeetCode Add Digits
- [LeetCode] Add Digits
- leetcode 258: Add Digits
- LeetCode题解:Add Digits
- leetcode: Add Digits
- 258Add Digits - LeetCode
- leetCode #258 Add Digits
- [LeetCode 258] Add Digits
- LeetCode: Add Digits
- LeetCode:Add Digits
- leetcode--add digits
- leetcode Add Digits
- leetcode--Add Digits
- leetcode Add Digits
- count(字段)不统计null值
- 枚举定义
- AdaultBird--我的黑马程序员之路!Chapter12---面向对象的三大特性!
- Notes on Caffe layers
- 栈的入栈与出栈的序列
- leetcode add digits
- 排序方法性质总结
- Leetcode: H-Index II
- 自定义控件使用DrawableLeft/Right图片和文字同时居中
- 【Linux】Ubuntu14.04 用root登录,亲测好用
- 字符设备驱动程序
- Windows批处理学习之文件操作类命令
- 快排
- 选择问题(查找第k小元素)