题意:

  将一个整数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. “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第三篇(登录窗口的实现以及如何保存登录者的信息)

    一.前言: 1.再看这篇文章的时候,您是否已经完成前两篇介绍的文章里的功能了?(Tabs页的添加,Tabs页右键的关闭,主题的更换)                 2.今天来说说登录窗口吧,看截图: ...

  2. java线程基础知识----线程与锁

    我们上一章已经谈到java线程的基础知识,我们学习了Thread的基础知识,今天我们开始学习java线程和锁. 1. 首先我们应该了解一下Object类的一些性质以其方法,首先我们知道Object类的 ...

  3. 在Android中使用Protocol Buffers(下篇)

    本文来自网易云社区. FlatBuffers编码数组 编码数组的过程如下: 先执行 startVector(),这个方法会记录数组的长度,处理元素的对齐,准备足够的空间,并设置nested,用于指示记 ...

  4. [CentOS7] firewalld重启失败 Failed to start firewalld - dynamic firewall daemon.

    错误信息: Failed to start firewalld - dynamic firewall daemon. 如图: 解决方法: 也就是kernel不支持现在的firewall防火墙的某些模块 ...

  5. Educational Codeforces Round 52F(树形DP,VECTOR)

    #include<bits/stdc++.h>using namespace std;int n,k;vector<int>son[1000007];int dp[100000 ...

  6. poj2763(lca / RMQ + 线段树)

    题目链接: http://poj.org/problem?id=2763 题意: 第一行输入 n, q, s 分别为树的顶点个数, 询问/修改个数, 初始位置. 接下来 n - 1 行形如 x, y, ...

  7. ELK系列(5) - Logstash怎么分割字符串并添加新的字段到Elasticsearch

    问题 有时候我们想要在Logstash里对收集到的日志等信息进行分割,并且将分割后的字符作为新的字符来index到Elasticsearch里.假定需求如下: Logstash收集到的日志字段mess ...

  8. 牛客练习赛41D(思维转化)

    AC通道 要点 思路:题解中将所求进行转化\[max\{相似度\} = max\{M-不相似度\} = M-min\{不相似度\}\]因此转化为求某01串T与所给众S串的最小不相似度,而最终答案是选取 ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)

    传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...

  10. ACM-ICPC 2018 徐州赛区网络预赛-G Trace(线段树的应用

    Problem:Portal传送门 Problem:Portal传送门  原题目描述在最下面.  我理解的题意大概是:有n次涨潮和退潮,每次的范围是个x×y的矩形,求n次涨退潮后,潮水痕迹的长度.   ...