【一天一道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. ...
随机推荐
- QCA4028软件平台启用双WAN指导
1 为何要启用双WAN QCA4028的硬件方案,基板上部署了一个LTE模块插槽,同时又外留了一个USB3.0接口,因此,就可以在此硬件平台上调试基于LTE的双WAN,预期实现: A 链路备份,在任意 ...
- cocos2d-x-3.0beta2创建项目遇到“UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 9: ordinal not in range(128)”的问题
在Windows平台下 用cocos2d-x-3.0beta2版本下的create_project.py工具创建项目 但是遇到如下问题:UnicodeDecodeError: 'ascii' code ...
- Python中高阶函数讲解
高阶函数讲解 1. 常规高阶函数 递归函数 格式:def func_name(variable): '''__doc__'''#函数的说明文档 if 条件表达式:#限制递归退出值 pass retur ...
- 2018年4月更新70多个公司dnc招聘职位
2018年4月更新70多个公司dnc招聘职位 请在本页回复,补充dnc招聘信息.公司案例 dnc简介 dnc = .NET Core.dotnet Core简写 dnc是微软新一代主力编程平台,开源. ...
- SUSE10的虚拟机安装以及ORACLE 11g的安装
SUSE10虚拟机安装与ORACLE安装 作者:张欣橙 本文所需要的所有参数均位于文末附录中 一.SUSE10虚拟机的安装与创建 新建虚拟机安装 选择下一步 选择下一步 选择下一步 选择下一步 选择下 ...
- centos 7 破解密码
CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例. 1.重启开机按esc 2.按e ...
- docker环境 快速使用elasticsearch-head插件
docker环境 快速使用elasticsearch-head插件 #elasticsearch配置 #进入elk容器 docker exec -it elk /bin/bash #head插件访问配 ...
- 在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会。骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天。要求尽早聚会
在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会.骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天.要求尽早聚会 ...
- bitbucket添加ssh key
右上角头像->bitbucket settings->ssh keys 直接点击: https://bitbucket.org/account/user/.../ssh-keys/ 教程: ...
- linux 服务器网络有关的内核参数
几乎所有的内核模块,包括内核核心模块和驱动程序,都在/proc/sys 文件系统下提供了某些配置文件以提供用户调整模块的属性和行为.通常一个配置文件对应一个内核参数,文件名就是参数的名字,文件的内容是 ...