[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
.
Since2
has 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 ...
随机推荐
- dubbo 自定义 Filter
通过自定义 Filter,可以在 dubbo 调用链中加入特定的逻辑,比如埋点分析调用链. 1. 新建 Filter 类 // @Activate(group = {Constants.CONSUME ...
- MySql多个count查询
现有一个student表结构数据如下: id hight sex age 1 160 0 16 2 170 1 16 3 180 1 17 4 160 1 16 5 170 ...
- git开发过程的配置和使用
git开发过程的使用 1.创建仓库 2.新建项目,填写项目名称等信息 3.初始化仓库,创建git仓库 git init 4.配置个人信息(配置过可忽略) git config --global use ...
- 【Jmeter_WebService接口】对项目中的GetProduct接口生成性能脚本
一.环境信息 https://xxx.xxx.svc?wsdl 用户名:username 密码:password 对其中的GetProduct接口进行测试 二.通过soapui获取soup请求信息 1 ...
- 003-RHEL7-Linux系统维护管理命令使用
系统维护管理命令: date 查看日期,设置日期 只有超级用户才能用date命令设置时间 date --help 显示时间的帮助命令 date{选项} 显示时间格式(以+开头,后面接时间格式) ...
- sql取大的一个值
select b.*, a.recid, a.keyno from product b, (select pcode, ...
- 尚学堂java答案解析 第一章
本答案为本人个人编辑,仅供参考,如果读者发现,请私信本人或在下方评论,提醒本人修改 一.选择题: 1.C 解析:java为了安全,中并没有引入C语言的指针概念. 2.AD 解析:B:Java先通过ja ...
- POJ 3436 ACM Computer Factory 最大流,拆点 难度:1
题目 http://poj.org/problem?id=3436 题意 有一条生产线,生产的产品共有p个(p<=10)零件,生产线上共有n台(n<=50)机器,每台机器可以每小时加工Qi ...
- js中 offset /client /scroll总结
offset家族(只能读取,不能操作): offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(就是子元素左边框到父元素左边框的距离). offsetTo ...
- oracle中的对象创建及删除语句【原创】
oracle对象 1.表 a)创建表1 create table students( id number(10), stuno number(10) , sex varchar2(2), age in ...