【一天一道LeetCode】#258. Add Digits
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
(二)解题
题目大意:给定一个非负数,对每一位进行相加,得到的和继续位相加,直到和为一位数为止
解题思路一
一开始,顺着题目的意思写下如下循环:
class Solution {
public:
int addDigits(int num) {
int ret = num;
while(ret>=10)
{
int sum = 0;
int temp = ret;
while(temp){//每次计算每一位上的和
sum+=temp%10;
temp/=10;
}
ret = sum;
}
return ret;
}
};
循环很简单,计算每一位的和,判断和是否为一位数,依次循环。
后来,突然看到题目中写了:Could you do it without any loop/recursion in O(1) runtime?
什么?O(1)时间!不用循环!下面看解法二的思路。
解题思路二
既然不用循环,就开始找规律,发现一直是1-9在循环,所以很容易想到mod9
要注意以下两个特殊情况:
(1) 0的时候为0
(2) mod9==0,此时返回9(除0外)
class Solution {
public:
int addDigits(int num) {
if(num==0) return 0;//特殊情况0
return num%9==0?9:num%9;
}
};
【一天一道LeetCode】#258. Add Digits的更多相关文章
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- LeetCode 258 Add Digits(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- [LeetCode] 258. Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- leetcode 258. Add Digits(数论)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode: 258 Add Digits(easy)
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
随机推荐
- WINFORM中treeview 节点显示不全
在设置treeview节点时,出现如下显示不全的问题: 这个问题是由于我们在treeview任务中编辑节点时设置的字体大于我们在treeview属性中设置frot字体导致的. 所以只要将treevie ...
- mysql 拼接
SELECT RTRIM(CONCAT(belong_master_ip ,'(',host_name,')')) AS cloudIP FROM `cloud_master_cfg`
- YARN整理
YARN整理 1.YARN的介绍 是一个资源管理.任务调度的框架,主要包含三大模块: ResourceManager(RM):负责所有资源的监控.分配和管理 ApplicationMaster(AM) ...
- Linux 管理软件
公司的openfire先前运行在windows上的,但由于在windows上openfire内存机制问题,最多只能占用2GB内存,且时间稍微长久一些就会自动挂掉,用户无法登陆和连接,因此迁移到了Cen ...
- Page Object设计模式实践
Page Object模式是使用Selenium的广大同行最为公认的一种设计模式.在设计测试时,把元素和方法按照页面抽象出来,分离成一定的对象,然后再进行组织. Page Object模式,创建一个对 ...
- django模板语言中的extends,block和include
extends和block一起用 它们用于母版和子版的继承 在母版html中将一些需要替换的部分用{% block xxx %}...{% endblock %}括起来, 在子版html中,在第一行需 ...
- formData的实现
参考:https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest <!doctype ...
- Hadoop2动态调整Log级别-以datanode的heartbeat log为例
在Hadoop中,有些log信息在正常情况下是不打印出来的.比如datanode发送heartbeat的日志. 代码位于BPServiceActor#sendHeartBeat方法中,如下图: 由于默 ...
- 【SSH系列】hibernate映射 -- 一对一双向关联映射
开篇前言 上篇博文[SSH进阶之路]hibernate映射--一对一单向关联映射,小编介绍了一对一的单向关联映射,单向是指只能从人(Person)这端加载身份证端(IdCard),但是反过来,不能从身 ...
- springMVC源码分析--HttpRequestHandlerAdapter(四)
上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...