[LeetCode&Python] Problem 258. Add Digits
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?
class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
s=str(num)
while len(s)>1:
n=0
for i in s:
n+=int(i)
s=str(n) return int(s)
[LeetCode&Python] Problem 258. Add Digits的更多相关文章
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- [LeetCode&Python] Problem 415. Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- [LeetCode&Python] Problem 788. Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode】 258. Add Digits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...
- [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 ...
随机推荐
- ActiveMQ 中的链表
ActiveMQ 中的消息在内存中时,以链表形式保存,以 PendingList 表示,每一个消息是 PendingNode. PendingList 主要有2种实现:OrderedPendingLi ...
- Mysql按日期分组(group by)查询统计的时候,没有数据补0的解决办法
转载自:http://blog.csdn.net/jie11447416/article/details/50887888 1.案例中的数据结构和数据如下 2.在没有解决的时候,是这样的 SELE ...
- JAVA项目之苹果IAP内购JAVA服务器验证流程详解
1.前言 本博客是经历过多个项目检验的, 绝对真实, 适应于对苹果iap内购稍微有些了解的JAVA开发人员, 认真看, 定能完美解决苹果内购问题. 苹果IAP内购支付实际上是"将客户端支 ...
- 和的java程序
- nginx+vue刷新404
问题原因:刷新页面时访问的资源在服务端找不到,因为vue-router设置的路径不是真实存在的路径.如上的404现象,是因为在nginx配置的根目录/Data/app/xqsj_wx/dist下面压根 ...
- Python Django 之 直接执行自定义SQL语句(一)
一.执行自定义SQL方法 1.Executing custom SQL directly 直接执行自定义SQL,这种方式可以完全避免数据模型,而是直接执行原始的SQL语句. 2.Manage ...
- day22 模块_1
核能来袭--模块 1.简单了解模块 2.Collections 3.Time模块 4.functools 一.初识模块 其实之前写的每一个PY文件都是一个模块 还有一些我们一直在使用的模块 buil ...
- 【转】Mac OS X Terminal 101:终端使用初级教程
最近学习苹果认证的<Mac OS X Support Essentials>教程,看到 Command Line 一节有很多实用的知识,下面选取一部分翻译 + 笔记,整理成此文. 你可以整 ...
- Instruments leak黑魔法定位内存泄漏
leak是一款很赞的内存检查的工具,但在使用的过程中有点繁琐,至少有些底层的泄漏笔者还是不知道如何下手 下面介绍一下简单leak的使用: 首先你要确认你的target不会被拒绝,确保profile是d ...
- 小程序之setData特殊情况 三种情况的wx:if
比如data{ “a”:{}, "b":{} } 你想完成这样的结构 //创建一个对象 var readyData={} //对象[key] =另一个对象 readyData[ke ...