[leetcode] Add Digits
2015-08-16 17:10
330 查看
from : https://leetcode.com/problems/add-digits/
Given a non-negative integer
result has only one digit.
For example:
Given
only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
思路:
任意x,x为n位数,则
![](http://img.blog.csdn.net/20150818090344729)
那么,
![](http://img.blog.csdn.net/20150818090443453)
又因为x%9 < 9, 所以,x%9即为所求。
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
2has
only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
思路:
任意x,x为n位数,则
那么,
又因为x%9 < 9, 所以,x%9即为所求。
public class Solution { public int addDigits(int num) { return (num-1)%9+1; } }
相关文章推荐
- 【C语言经典实例】-指向结构体的指针变量
- 精易编程助手
- shell脚本:正则表达式-初
- Vuforia 摄像头自动对焦问题测试
- log4net的使用 日志输出
- BASE64Decoder小解
- UVa 1586 - Molar mass
- BASE64Decoder小解
- [洛谷2415]集合求和
- sass、git、ruby的安装与使用。
- 华为OJ(密码强度等级)
- c++ 类的赋值运算符=的重载,以及深拷贝和浅拷贝
- ibatis传递多参数的方法
- 12219 - Common Subexpression Elimination(表达式树)
- [LeetCode] Palindrome Partitioning
- 华为OJ(求最大连续bit数)
- HW—字符串最后一个单词的长度,单词以空格隔开。
- win10初体验,我的错误代码哪里去了
- C++基础---对象数组中delete与delete[]的区别
- HDU 1013.Digital Roots【模拟或数论】【8月16】