【一天一道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. ...
随机推荐
- 勤拂拭软件Android开发之旅(1) 之 Android 开发环境搭建
勤拂拭软件工作室原创出品,欢迎转载,欢迎交流. 转载请注明原文:http://www.cnblogs.com/wangleiblog/p/6019063.html 勤拂拭软件Android开发之旅目录 ...
- glusterfs 4.0.1 rpc 分析笔记1
Jimmy的文档:Glusterfs的rpc模块分析 第一节.rpc服务器端实现原理及代码分析 第二节.rpc客户端实现原理及代码分析 第三节.rpc通信过程分析 经过阅读源码对比之前提及的文档,我个 ...
- 如何用Netty实现一个轻量级的HTTP代理服务器
为什么会想通过Netty构建一个HTTP代理服务器?这也是笔者发表这篇文章的目的所在. 其主要还是源于解决在日常开发测试过程中,一直困扰测试同学很久的一个问题,现在我就来具体阐述一下这个问题. 在日常 ...
- python map filter reduce的优化使用
这篇讲下python中map.filter.reduce三个内置函数的使用方式,以及优化方法. map()函数 map()函数会根据提供的函数对指定序列做映射. 语法: map(function,it ...
- angular学习笔记 父子组件传值
一.如何将父组件的值传到子组件? 在子组件里面引入Input,然后用@Input 变量1 接收 接着在父组件中的,子组件标签上添加[msg]="msg",中括号里的名字要与子组件中 ...
- Java 的异常处理机制
异常是日常开发中大家都「敬而远之」的一个东西,但实际上几乎每种高级程序设计语言都有自己的异常处理机制,因为无论你是多么厉害的程序员,都不可避免的出错,换句话说:你再牛逼,你也有写出 Bug 的时候. ...
- Vue 波纹按钮组件
代码链接:https://github.com/zhangKunUserGit/vue-component 效果图: 大家可以在线运行: https://zhangkunusergit.github. ...
- MongoDB 高级索引
考虑以下文档集合(users ): { "address": { "city": "Los Angeles", "state&qu ...
- self关键字
self关键字 self:当前类/对象的指针(指向当前对象/方法调用者) 作用1 当类里有变量名和成员变量名一样的时候,可以使用self区分 例: 我们写一个人的类,有一个年龄属性,在get方法里,我 ...
- Android简易实战教程--第四十三话《上拉加载与下拉刷新》
ListView的下拉刷新很常见,很多开源的框架都能做到这个效果,当然也可以自己去实现.本篇案例是基于xlistview的. 布局: <RelativeLayout xmlns:android= ...