问题描述:

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. 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...

  2. centos 问题解决记录

    在centos上用pip安装包,显示成功安装,但是用pip list去看发现实际上并没有安装? 安装用的是pip install xxx 是不行的,需要用sudo pip install xxx就可以 ...

  3. Xpath表达式的粗介绍

    关于在自动化中Xpath表达式的书写,其实我也只是刚刚入门,粗略的跟着网上的教程学了一下,这篇我就来分享总结一下我学习到的知识. 首先呢,我们先认识一下什么是Xpath.Xpath是XML路径语言,它 ...

  4. MySQL 查询语句--------------进阶5:分组查询

    #进阶5:分组查询 /* select 分组函数,列(要求出现在group by的后面) from 表 [where 筛选条件] group by 分组的列表 [order by 子句] 注意: 查询 ...

  5. 让 Visio 2003/2007 同时开多个独立窗口

    1. 打开 Visio 2003/2007 2. 点击菜单[工具] -> [选项]: 3. 在弹出的“选项” 对话框中选择“高级”选项页: 4. 去掉“在同一窗口中打开每一 ShapeSheet ...

  6. 关于eclipse中的maven插件问题

    最近上课讲eclipse 中的maven插件 有一个坑确实比较坑,实际上就是一个配置的原因. 就是在eclipse中设置java 的buildpath的时候,一般不注意往往都设置成了jre的,这样的话 ...

  7. jQuery DataTables 分页

    HTML:================================================================== <div class="ibox-con ...

  8. decision table

    某研究所重新对其在大学以上学历的职工安排工作. 其方针如下:" 如果年龄不满18岁,文化程度是大学,若是男性,则一律要求考研究生.若是女性,则分配到研究所办公室任行政干部. 如果年龄满18岁 ...

  9. HTML5--sessionStorage、localStorage、manifest

    sessionStroage: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  10. Linux中ssh及scp的连接

    1. 当你想获取另外一台电脑上的数据时,可以使用这个命令 scp -P 10022 root@172.30.83.173:~/ubuntu1.tar ./ -r   代表传输文件夹,直接传文件可以不加 ...