题意:

  将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num。

思路:

  注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0。

  规律在于随着所给自然数num的递增,结果也是在1~9内循环递增的,那么结果为(num-1)%9+1。

  

C++:

 class Solution {
public:
int addDigits(int num) {
if(!num) return ;
else return (num-)%+;
}
};

AC代码

python:

 class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
return 0 if not num else (num-1)%9+1

AC代码

LeetCode Add Digits (规律题)的更多相关文章

  1. [LeetCode] Add Digits (a New question added)

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  2. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  3. LeetCode——Add Digits

    Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...

  4. (leetcode)Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  6. 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 ...

  7. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

  8. LeetCode 258. 各位相加(Add Digits)

    258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...

  9. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

随机推荐

  1. 《鸟哥的Linux私房菜》读书笔记3

    1. bash shell变量设定: name=myname,(不加空格) echo $name 注意: 若myname中有空格,则用单引号或者双引号("内特殊字符保留变量特性,'内特殊字符 ...

  2. go语言实战教程:项目文件配置和项目初始化运行

    在上节内容中,我们已经搭建了实战项目框架,并将实战项目开发所需要的静态资源文件进行了导入.在本节内容中,我们将讲解如何通过相关的配置,并初始化运行项目. conf配置文件读取配置信息 我们前面说过,使 ...

  3. macbook 集成 Kaleidoscope 环境

    Kaleidoscope 在macbook ,算是非常出色的file diff 工具了,唯一一个缺点就是:贵. 在网上找了一个科学实用版,暂时先这么用着吧(此处强烈建议有条件的朋友,支持正版). 作者 ...

  4. Tomcat&Servlet

    Tomcat&Servlet 一.web开发相关的概念 1. 软件架构 1.1 C/S架构 C:Client客户端, S:Server服务器 比如:QQ.微信.大型网游 优点: 显示效果炫 安 ...

  5. Java 8 Optional类使用的实践经验

    前言 Java中空指针异常(NPE)一直是令开发者头疼的问题.Java 8引入了一个新的Optional类,使用该类可以尽可能地防止出现空指针异常. Optional 类是一个可以为null的容器对象 ...

  6. windows cmd命令 mkdir生成多个文件bug问题

    [问题现象] 有这样一个bat脚本,目的是为了根据时间创建文件夹 执行后却发现生产的文件夹有两个,名字被分开了,很是纳闷,一度以为自己哪里写错了 [问题原因] 经过查阅资料,一点一点的定位.发现是因为 ...

  7. 继承、super、this、抽象类

    继承.super.this.抽象类 继承.super.this.抽象类 继承.super.this.抽象类 继承.super.this.抽象类 继承.super.this.抽象类

  8. 用户与授权:MySQL系列之六

    一.用户管理 1.用户账号 用户的账号由用户名和HOST俩部分组成('USERNAME'@'HOST') HOST的表示: 主机名 具体IP地址 网段/掩码 可以使用通配符表示,%和_:192.168 ...

  9. iOS导航栏添加返回按钮的方式

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回上级界面" style:UIBarBu ...

  10. EcmaScript学习

    1.eval: ts: declare function eval(x: string): any; js: /** @param {*} x @return {Object} */ eval = f ...