问题描述:

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.
  Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

思路:

最暴力的思路是用循环,但是最后题目提示可以不用循环,以及运行时间O(1).

那么我们先来观察1到20的所有的结果:

1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8    
9    9    
10    1
11    2
12    3    
13    4
14    5
15    6
16    7
17    8
18    9
19    1
20    2

根据上面的列举,我们可以得出规律,每9个一循环,所有大于9的数的结果都是对9取余,其中9的倍数 对9取余就是0了,此时返回9;若非9的倍数,则返回余数。这里有个特殊情况,就是0, 0对9取余,得到的0,但是返回值却应该是0,所以这个需要特殊处理下。

代码:

 class Solution:
def addDigits(self, num: int) -> int:
return 9 if num != 0 and num % 9 == 0 else num % 9

Python3解leetcode Binary Tree PathsAdd Digits的更多相关文章

  1. Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes

    问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  2. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  3. Python3解leetcode Same Tree

    问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

  4. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  5. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  6. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  7. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  8. Python3解leetcode Average of Levels in Binary Tree

    问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  9. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

随机推荐

  1. 设计模式 - 装饰器模式(Decorator)

    简介 场景 通过继承和关联都可以给对象增加行为,区别如下: 继承是静态的(无法在程序运行时动态扩展),且作用于所有子类.硬编码,高耦合. 通过装饰器可以在运行时添加行为和属性到指定对象.关联关系就是在 ...

  2. Discuz升级 Database Error : pre_common_syscache ADD PRIMARY KEY (cname)【解决办法】

    错误码: 1068Multiple primary key defined Execution Time : 00:00:00:000Transfer Time : 00:00:00:000Total ...

  3. 《STL源码剖析》——第七、八章:仿函数与接配器

    第七章:仿函数  7.1.仿函数(函数对象)概观 STL仿函数的分类,若以操作数(operand)的个数划分,可分为一元和二元仿函数,若以功能划分,可分为算术运算(Arithmetic).关系运算(R ...

  4. Python入门习题10.河内塔(汉诺塔)问题

    例10 共n个圆盘,a,b,c三根柱子 #汉诺塔问题.py def Hanoi(n): #定义n阶汉诺塔问题移动次数函数 if n == 1: return 1 else: return 2*Hano ...

  5. Codeforces 1091C (数学)

    题面 传送门 分析 假设k是固定的,那访问到的节点编号就是\(1+(a·k \mod n )\),其中a为正整数. 通过找规律不难发现会出现循环. 通过题目中的图片我们不难发现 只有k=1,2,3,6 ...

  6. Codeforces - 1194F - Crossword Expert - 组合数学

    https://codeforc.es/contest/1194/problem/F 下面是错的. 看起来有点概率dp的感觉? 给你T秒钟时间,你要按顺序处理总共n个事件,每个事件处理花费的时间是ti ...

  7. P5445 [APIO2019]路灯(树套树)

    P5445 [APIO2019]路灯 转化为平面上的坐标(x,y),set维护连续区间. 用树套树维护矩阵加法,单点查询. 注意维护矩阵差分的时候, $(x,y,v)$是对$(x,y)(n+1,n+1 ...

  8. Vue / keep-alive使用

    keep-alive keep-alive是Vue提供的一个抽象组件,用来对组件进行缓存,从而节省性能,由于是一个抽象组件,所以在v页面渲染完毕后不会被渲染成一个DOM元素 <keep-aliv ...

  9. 返回与Table结构相同的DataTable副本

    /// <summary> /// 返回与Table结构相同的DataTable副本 /// </summary> public static DataTable getStr ...

  10. 安卓构架组件——向项目添加组件(Adding Components to your Project)

    在开始之前,建议阅读 应用架构指南. Before getting started, we recommend reading the Architecture Components Guide to ...