leetcode--Add Digits
来源:互联网 发布:w2003办公软件下载 编辑:程序博客网 时间:2024/05/17 06:38
题目: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?
public class Solution { public int addDigits(int num) { while(num>=10){ int sum = 0; while(num!=0){ sum += num % 10; num /= 10; } num = sum; } return num; }}
Two:不用循环,注意:num的返回值是除以9的余数。
public class Solution { public int addDigits(int num) { return num==0 ? 0 : (num % 9 == 0 ? 9 : (num % 9)); }}
0 0
- [leetcode][math] Add Digits
- [leetcode] Add Digits
- 【leetcode】Add Digits【java】
- Leetcode: Add Digits
- LeetCode Add Digits
- [LeetCode] Add Digits
- leetcode 258: Add Digits
- LeetCode题解:Add Digits
- leetcode: Add Digits
- 258Add Digits - LeetCode
- leetCode #258 Add Digits
- [LeetCode 258] Add Digits
- LeetCode: Add Digits
- LeetCode:Add Digits
- leetcode--add digits
- leetcode Add Digits
- leetcode--Add Digits
- leetcode Add Digits
- 【AngularJS探索之旅】入门篇
- 深度卷积对抗生成网络(DCGAN)
- 网页控制远程wifi继电器开关(远程内网开关)esp8266 物联网软件第39版
- [计算机领域的思维导图系列整理][java]think in java
- 29、(知识篇)SpringMVC06 Spring jstlview国际化/访问/web-inf/下的jsp/访问静态资源
- leetcode--Add Digits
- mvc配置类(日志类)的编写
- jQuery–20个的jQuery效果
- poj2001
- 使用Jquery设计列表
- nbt
- ubuntu安装hadoop2.7.3集群
- android集成支付宝教程
- c++Lesson03 引用