题目:

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?

Show Hint

题目分析:

首先觉得例子很明显,可能的结果是数目固定的(0~9),那么很有可能是由规律的,加上后面的一句,要求在常数范围内做出来,那么很可能有很大的规律,从0~30总结 一下规律

总结得出结论是从1~9循环

我在审题时候,遗漏了非负数,包括0,没有考虑0的特殊情况

代码:

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

leetcode之旅(6)-Add Digits的更多相关文章

  1. LeetCode 258. 各位相加(Add Digits)

    258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...

  2. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  3. [LeetCode&Python] Problem 258. Add Digits

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

  4. LeetCode(258) Add Digits

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

  5. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

  6. 【LeetCode】Add Digits

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

  7. 【LeetCode】258. Add Digits (2 solutions)

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

  8. [LeetCode] Add Digits (a New question added)

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

  9. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  10. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

随机推荐

  1. UNIX网络编程——原始套接字的魔力【续】

    如何从链路层直接发送数据帧 上一篇里面提到的是从链路层"收发"数据,该篇是从链路层发送数据帧. 上一节我们主要研究了如何从链路层直接接收数据帧,可以通过bind函数来将原始套接字绑 ...

  2. MAMP显示文件列表

    背景 MAMP是Mac下的一个PHP+Nginx+MySQL的集成环境,支持多站点,不同版本PHP. 今天有人请教MAMP如何显示文件列表的问题,这里记录一下. 知识补充 一个网站为了安全考虑,默认是 ...

  3. JVM的GC(概念与深入)

    深入浅出了解什么是GC: http://my.oschina.net/xianggao/blog/86985 GC策略详解: http://blog.csdn.net/winniepu/article ...

  4. 最简单的基于FFmpeg的内存读写的例子:内存播放器

    ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...

  5. IBM SPSS 实习总结

    2015过完年,我知道导师要出国了,自己也算是水了一个idea 的论文.希望研二能找个实习,早听说西安IBM这边有学长在里面实习过,2月底联系了一下简历就塞了过去.面试就在锦业一路软件园他们上班的地方 ...

  6. OC语言编写:为视图添加丝滑的水波纹

    先看一下最终效果图: 首先我们可以把如此丝滑的水波纹拆分一下下: 一条规律的曲线. 曲线匀速向右移动. 曲线下方的位置用颜色填充. 于是先来一条曲线吧. 对于需要产生波动如此规律的曲线,我们首先想到的 ...

  7. 分布式系统中的RPC请求经常出现乱序的情况 写一个算法来将一个乱序的序列保序输出

    分布式系统中的RPC请求经常出现乱序的情况.  写一个算法来将一个乱序的序列保序输出.例如,假设起始序号是1,对于(1, 2, 5, 8, 10, 4, 3, 6, 9, 7)这个序列,输出是:  1 ...

  8. Java 中Log4j的使用详情

    Log4j 真的很简单,简单到令人发指的地步.不是要记录日志吗?那就给你一个Log ,然后你用Log 来写东西就行了,先来一个完整类示例: package test; import org.apach ...

  9. Miscellaneous Articles

    标记一下,慢慢看  http://www.oracle-base.com/articles/misc/articles-misc.php Miscellaneous Articles DBA Deve ...

  10. 海量数据挖掘MMDS week2: 频繁项集挖掘 Apriori算法的改进:非hash方法

    http://blog.csdn.net/pipisorry/article/details/48914067 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...