Leetcode49: Add Digits
2015-09-07 16:33
134 查看
Given a non-negative integer
one digit.
For example:
Given
only one digit, return it.
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.
class Solution { public: int addDigits(int num) { int sum = num; while(sum >= 10) { num = sum; sum = 0; while(num) { sum += num%10; num /= 10; } } return sum; } };
相关文章推荐
- Hadoop基本命令
- 基于PL/SQL的数据库备份方法
- iptables命令
- 通过meta标签改变浏览器内核做兼容
- TreeList<下>
- 微信网页授权
- 如何在一个页面上让多个jQuery版本共存
- EhCache入门
- matlab函数——shading函数
- EventBus集成和使用
- ffmpeg 详细命令列表
- 购物车功能实现 语言: php+MySQL+jQuery+Ajax
- HBase学习笔记(一)———《HBase简介》
- hdu 4284 Travel 2012天津网络赛 状压dp TSP 最短路
- HTML图片拉伸
- android 百度地图 点击选中到最上层 置顶
- ViewPager切换时粘滞视差的效果--模仿美丽说HIGO全球买手圈的滑动效果
- [推荐]Java开发中的23种设计模式
- IONIC屏幕方向锁定
- 一个奇怪的查询结果