【一天一道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. ...
随机推荐
- Linux学习之CentOS(二十)------vi/vim 按键说明
vi/vim 按键说明 除了上面简易范例的 i, Esc, :wq 之外,其实 vim 还有非常多的按键可以使用. 第一部份:一般模式可用的光标移动.复制粘贴.搜索替换等 移动光标的方法 h 或 向左 ...
- Mysql优化--慢查询日志
Mysql 系列文章主页 =============== 默认没有开启慢查询日志功能.如果不是调优需要的话,一般不建议开启. 查看是否开启慢查询日志: SHOW VARIABLES LIKE '%sl ...
- C语言实现的排序
冒泡排序 比较相邻的两个元素,若顺序不对,则将其调换 通过一遍排序,较大的数会排到最后(沉到底部) 两层循环,外层循环控制遍数,内层循环控制每一遍内的排序. 完整代码: #include<std ...
- C++笔记--1
一.namespace 命名空间 //定义一个命名空间 namespace spaceA { ; } int main(void) { //调用方式一 using namespace spaceA; ...
- 阿里架构师带你深入浅出jvm
本文跟大家聊聊JVM的内部结构,从组件中的多线程处理,JVM系统线程,局部变量数组等方面进行解析 JVM JVM = 类加载器(classloader) + 执行引擎(execution engine ...
- 无忧代理免费ip爬取(端口js加密)
起因 为了训练爬虫技能(其实主要还是js技能-),翻了可能有反爬的网站挨个摧残,现在轮到这个网站了:http://www.data5u.com/free/index.shtml 解密过程 打开网站,在 ...
- ACM hdu 3336 Count the string
[题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...
- python 函数运算先于单目运算
>>> def f(): >>> -f() - 初一看,-f()比较陌生的样子,细想,这是合理的
- Thread 方法
Thread类的一些被Thread对象调用的方法: 1 public void start() 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 2 public void run() ...
- Spark-1.6.0之Application运行信息记录器JobProgressListener
JobProgressListener类是Spark的ListenerBus中一个很重要的监听器,可以用于记录Spark任务的Job和Stage等信息,比如在Spark UI页面上Job和Stage运 ...