LeetCode Add Digits (规律题)
题意:
将一个整数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 (规律题)的更多相关文章
- [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. ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode——Add Digits
Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...
- (leetcode)Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 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 ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
随机推荐
- hdu1079
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int mai ...
- hdu1072
#include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...
- swift中的类拓展 extension
以添加颜色为例,new 一个swift文件夹 不是cocoa类 也不需要继承什么. 然后 import UIKit protocol ColorDalegate{ class func mainCol ...
- SQL笔记:基础篇
1.BETWEEN AND (查询某个区间的数据) 例如:查询user表中年龄在15-30岁的人 SELECT * FROM user WHERE age between 15 and 30 2.IN ...
- Spring 属性配置
此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 随着Spring的不断发展与完善,早期它的功能可能只看做是IOC(反转控制)的容器,或者其最大的亮点为DI( ...
- 对zabbix监控磁盘性能的补充
原因 在上一篇文章中,我写了完整的磁盘监控步骤,希望对大家有所帮助.但是这里还需要作出一点补充. 根据上一篇文章的内容,我是使用iostat命令不停的收集磁盘的信息,然后写入到/tmp/iostat_ ...
- ThinkSNS+ PHP开发概述
Plus (读音:[plʌs],全称:ThinkSNS+ [θɪŋk es en es plʌs],是 ThinkSNS 系列产品一个重要版本,其软件识别名称为 Plus 即 +) 是一个基于 Lat ...
- P3379 【模板】最近公共祖先(LCA)(倍增)
这题有毒!!!!!!!!!! TM我重新打的板子,然而...... 5分钟打完 debug两小时 我的写法常数太大了 每次DFS都要For去更新F 最后写了快读才A 改: 只处理f[i][0] dfs ...
- web综合案例03
web综合案例03 web综合案例03 web综合案例03 web综合案例03 ... 待复习
- thinkphp5.1常量定义使用
thinkphp5.1取消了系统常量 可以把常量配置在app.php文件中 //配置网站地址 'WEB_URL'=>'http://127.0.0.1/tp5', 可以使用config()函数直 ...