题意:

  将一个整数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. 简单的html兼容(参考js和css的常规写法)

    参考往常css/js的浏览器选择加载 <!--[if lte IE 8]> <link rel="stylesheet" href="IEBrower. ...

  2. RedisUtil(未完,持续更新中....)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. php用百度地图API进行逆地址解析

    <?php /** * 根据地理坐标获取国家.省份.城市,及周边数据类(利用百度Geocoding API实现) * 百度密钥获取方法:http://lbsyun.baidu.com/apico ...

  4. 【leetcode 239. 滑动窗口最大值】解题报告

    思路:滑动窗口的思想,只要是求连续子序列或者子串问题,都可用滑动窗口的思想 方法一: vector<int> maxSlidingWindow(vector<int>& ...

  5. select下拉框如何与接口配合动态生成option选项

    前几天在做任务时考虑到这个问题,具体任务用例如下: HTML: JS:

  6. Mysql导入导出数据库11111

    导出: 通过命令行  在MYSQL中的bin文件夹的目录下 输入:D:\phpStudy\MySQL\bin>mysqldump -uroot -p 数据库名 > 导出的文件名 导入: 需 ...

  7. 51nod1064(Bash博弈)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1067 题意:中文题诶~ 思路:直接规律就好了... 代码: ...

  8. Jmeter-返回值乱码处理

    Jmeter安装目录/bin/jmeter.properties中sampleresult.default.encoding默认为ISO-8859-1,将参数修改为 sampleresult.defa ...

  9. thinkphp5控制器访问转换问题

    假设定义了HelloWorld控制器 url访问地址就是:http://localhost/index.php/index/hello_world,与此同时view目录下的模板文件夹要命名为hello ...

  10. IOS 关于tableview中cell的长按手势

    说明:虽然是tableview中cell的长按手势  但是手势是添加在tableview上的 UILongPressGestureRecognizer *longpress = [[UILongPre ...