题意:

  将一个整数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. CF622F The Sum of the k-th Powers(拉格朗日插值)

    题意 给出 \(n,k\) , \(n\le10^9,k\le10^6\) ,求 \(\sum_{i=1}^n i^k(mod\;10^9+7)\) 题解 自然数幂次和,是一个\(k+1\)次多项式, ...

  2. 洛谷 P2024 [NOI2001]食物链 (并查集)

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P2024 这道题和团伙这道题的思想比较类似,都是一个数组分成几个集合,但这道题的思路更加混乱,建议没做 ...

  3. 最长双回文串(模板+dp)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ , f ...

  4. SVN历史版本比较中文乱码

    将Workspace的编码改为UTF-8即可,详见上图:

  5. Luogu P3265 [JLOI2015]装备购买

    好吧刚开始不知道自己在写什么,,,后来写了线性方程组,又过了一天一上午终于明白了... 当然题意很显然:求代价最小的极大线性无关组. 那就高斯消元(好吧刚开始我不会用它来解这道题qwq) 第一个循环是 ...

  6. 牛客假日团队赛1 J.分组

    链接: https://ac.nowcoder.com/acm/contest/918/J 题意: 在Farmer John最喜欢的节日里,他想要给他的朋友们赠送一些礼物.由于他并不擅长包装礼物,他想 ...

  7. 牛客假日团队赛1 G.Superbull

    链接: https://ac.nowcoder.com/acm/contest/918/G 题意: Bessie and her friends are playing hoofball in the ...

  8. ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret = 2) from /usr/local/etc/php-fpm.conf at line WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf'

    Building from source is not easy if something is a bit different, and I had a hard time with some di ...

  9. mybatis批量处理sql

    转载大神 https://www.cnblogs.com/xujingyang/p/8301130.html

  10. remote error: You can't push to git 解决办法