Add Digits

原创 2016年06月02日 00:24:42

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?

枚举1-20可发现所有的数字之间都是有规律的,以9为周期,不断循环

public class Solution {
    public int addDigits(int num) {
         if(num %9 !=0)
         return num%9;
         if(num==0)
         return 0;
         return 9;

    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Add Digits

  • 2015-08-19 15:55
  • 376B
  • 下载

leetCode 258. Add Digits

题目链接:https://leetcode.com/problems/add-digits/     题目内容: Given a non-negative integer num, rep...

Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit....

LeetCode之路:258. Add Digits

LeetCode之路:258. Add Digits 一、引言这是一道比较难的题,之所以下这个定义,是因为要做出来这道题的正确答案,需要一点点数学知识。当然,能看到这篇博客的人,大部分都是程序员,那么...

<LeetCode OJ> 258. Add Digits

258. Add Digits My Submissions Question Total Accepted: 54029 Total Submissions: 113401 Difficul...

Leetcode - Math -258. Add Digits(数位求和,规律题)

1. Problem Description  Given a non-negative integer num, repeatedly add all its digits until the r...

LeetCode 258 Add Digits(数字相加,数字根)

翻译给定一个非负整型数字,重复相加其所有的数字直到最后的结果只有一位数。例如:给定sum = 38,这个过程就像是:3 + 8 = 11,1 + 1 = 2,因为2只有一位数,所以返回它。紧接着:你可...
  • NoMasp
  • NoMasp
  • 2015-12-24 10:17
  • 3300

Add Digits

Add Digit 标签:leetcode,C++

258. Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit...
  • iFuMI
  • iFuMI
  • 2016-03-22 21:22
  • 149
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)