LeetCode - Add Digits
2015-12-06 11:01
169 查看
题目:
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.
思路:
除和余
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.
思路:
除和余
package others; public class AddDigits { public int addDigits(int num) { if (num / 10 == 0) return num; int res = 0; while (num > 0) { res += num % 10; num = num / 10; } return addDigits(res); } public static void main(String[] args) { // TODO Auto-generated method stub } }
相关文章推荐
- qt5 解析Json文件
- Python学习第一天心得体会!!!
- java小说上传txt功能,(同名小说不能再次上传)
- 1049. Counting Ones (30)
- 1048. Find Coins (25)
- 规划和按安装邮件系统
- AngularJs ToolTips实现
- sqldeveloper
- 1047. Student List for Course (25)
- 1046. Shortest Distance (20)
- 协议模糊测试
- 【C语言提高26】二级指针做输入的第三种内存模型:手工打造二维内存
- Android触摸事件处理机制之requestDisallowInterceptTouchEvent
- java小说上传到txt功能实现(相同的小说不能再次添加)
- 干式双离合和湿式双离合的优缺点
- 1045. Favorite Color Stripe (30)
- 斯坦福大学机器学习课程原始讲义(含公开课视频) (转载)
- Android@id和@+id区别
- MySql重装或者重新配置数据库实例时遇到“本地计算机无法启动Mysql服务”错误的解决方法
- layout设置出现和隐藏动画