[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 ...
随机推荐
- CentOS中/英文环境切换教程(CentOS6.8)
一.前言 对于不习惯英文的人可能想将系统由英文转成中文:而对于考虑客户端如果没正确配置,中文目录可能显示为乱码的人则可能宁愿将系统由中文转成英文. 中文切换为英文,实际就是将LANG的值由zh_CN- ...
- Linux只下载不安装软件包
有时我们并不需要安装软件而只要下载软件包. 包格式 命令 命令所属包 命令下载格式 rpm yumdownloader yum-utils yumdownloader package_name deb ...
- ajax参数传递之[HttpGet]/[HttpPost]/[HttpPut]/[HttpDelete]请求
$.ajax({ type: "get", url: "http://localhost:27221/api/Charging/GetByModel", con ...
- ssh三大框架整合
spring+struts2+hibernate 参考1:数据库为oracle http://takeme.iteye.com/blog/1678268 参考2:数据库为mysql http://bl ...
- java 实现简单的链式栈
package com.my; /** * 链式栈 * @author wanjn * */ public class LinkedStack { private Node head; private ...
- 1449 - The user specified as a definer('xxx'@'%') does not exist
指定的用户不存在,创建相应的账户即可,注意主机那里填的内容,我的这个是@'%'所以不用填任何内容.
- rancher中使用ingress-lbs做负载均衡
rancher 相关资料 http://rancher.com/docs/rancher/v1.6/zh/kubernetes/ingress/ lvs, haproxy, nginx负载均衡器比较 ...
- Java伪代码之大道至简读后感
import.java.大道至简.*; import.java. 愚公移山.*; public class YuGongYiShan//定义一个名为YuGongYiShan的类 {//类定义的开始 S ...
- js 数组去重的几种方式及原理
let arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,' ...
- Arduino-汉王PM2.5检测模组B1
汉王PM2.5检测模组B1,接入Arduino,使用I2C1602显示屏显示 #include <Arduino.h> #include <Wire.h> #include & ...