LeetCode 258
Add Digits
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?
Hint:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
public class Solution {
public int addDigits(int num) {
return (num-1) % 9 + 1;
}
}
/*************************************************************************
> File Name: LeetCode258.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 10 May 2016 07:44:57 PM CST
************************************************************************/ /************************************************************************* Add Digits 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? Hint: A naive implementation of the above process is trivial.
Could you come up with other methods?
What are all the possible results?
How do they occur, periodically or randomly?
You may find this Wikipedia article useful. ************************************************************************/ #include <stdio.h> int addDigits( int num )
{
int ret = (num-) % + ;
return ret;
} int main()
{
int n = ;
int ret = addDigits(n);
printf("%d\n", ret);
return ;
}
LeetCode 258的更多相关文章
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
- Leetcode -- 258 数位相加
258. Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- 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. ...
- Java实现 LeetCode 258 各位相加
258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由 ...
- 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 ...
随机推荐
- HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)
NanoApe Loves Sequence Ⅱ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5806 Description NanoApe, t ...
- [C语言 - 6] static & extern
A. extern函数 一个c文件生成一个obj文件 外部函数:允许其他文件访问.调用的函数(默认函数为外部函数),不允许存在同名的外部函数 my.c //define a extern fu ...
- CodeForces 732B Cormen — The Best Friend Of a Man (贪心)
题意:给定n和k表示,狗要在任意连续两天散步次数要至少为k,然后就是n个数,表示每天的时间,让你增加最少次数使得这个条件成立. 析:贪心,策略是从开始到最后暴力,每次和前面一个相比,如果相加不够k,那 ...
- 认识JavaScript的原型
本来打算也写一个JavaScript学习笔记的系列,不过由于笔者不太想买大部头的js数据,和网上的资料也不少,所以js系列就打算写到了算了了. 要理解JavaScript就要理解其原型,首先我们先区分 ...
- 在VS中安装EF和项目引用EF
1.通过Visual Studio安装NuGet (1). 打开Visual Studio扩展管理器 (2). 选择联机库,并在搜索中写入NuGet,然后点击搜索结果中NuGet Packag ...
- 教你50招提升ASP.NET性能(十七):不要认为问题只会从业务层产生
(28)Don’t assume that problems can only arise from business logic 招数28: 不要认为问题只会从业务层产生 When beginnin ...
- CCLabelTTF、CCLabelAtlas和CCLabelBMFont的区别
转自:http://blog.sina.com.cn/s/blog_67a5e47201018tj8.html 在Cocos2d以及Cocos2d-x中,我们经常会用到CCLabelTTF以及CCLa ...
- BZOJ 1079: [SCOI2008]着色方案 记忆化搜索
1079: [SCOI2008]着色方案 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- qt里标识操作系统的宏
qt文件里的 windows的宏是Q_WS_WIN32/* The window system, must be one of: (Q_WS_x) MACX - Mac OS X ...
- Python_爬虫4
Python爬虫入门(8):Beautiful Soup的用法 上一节我们介绍了正则表达式,它的内容其实还是蛮多的,如果一个正则匹配稍有差池,那可能程序就处在永久的循环之中,而且有的小伙伴们也对写正则 ...