题目:

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. Editorial Board 、co-editor、ediitor、editor-in-chief的区别

    昨天更新掘金APP-IOS之后发现一个比较严重的Bug,联系管理者报告了Bug,中途发现掘金的发布功能需要申请成为co-editor才行. 那么这里科普一下这几个名词: Editorial Board ...

  2. Gem/Bundle/Rvm

    做过Ruby项目的人可能有过我一样的感受,rubygems.org在中国的访问太慢了,每次我们bundle install都要等老长时间,而我们通过浏览器去下载对应的gems文件时却速度刷刷的... ...

  3. UNIX网络编程——信号驱动式I/O

    信号驱动式I/O是指进程预先告知内核,使得当某个描述符上发生某事时,内核使用信号通知相关进程. 针对一个套接字使用信号驱动式I/O,要求进程执行以下3个步骤: 建立SIGIO信号的信号处理函数. 设置 ...

  4. Objc中触摸处理阻塞时消息派送的问题

    在游戏场景中添加了touchBegan的处理: -(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event{ if ([sel ...

  5. 关于USB驱动的软件测试方法

    在工作中难免会使用一些外部设备挂载到平台进行测试,比如U盘,那么判断一个U盘是否能正常读写的方法如下: 1.在U盘中放入一个二进制文件(xxx.bin) 2.通过U盘在软件上读取该二进制文件,并计算其 ...

  6. java linux ImageIO 验证码在一段时间以后出不来 问题总结

    最近在测试上布署的项目经常性的出现验证码过了一段时间以后出不来的情况,耐心找了一下,最后在上级的指导下发现了报错,其实说真的,我自己也找到了这个报错,只是没有当一回事.因为这个验证码的东西不是我写的, ...

  7. 最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)

    ===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...

  8. C# 运行时序列化

    一. 序列化与反序列的作用 为什么要有序列化呢,考虑下面这种情况,在WINFORM或者更为方便的WPF工程中,当我们进行UI设计时,可以随意的将一个控件剪切/张贴到另外一个地方.操作方便的背后是什么在 ...

  9. Chapter 2 User Authentication, Authorization, and Security(10):创建包含数据库

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/39473895,专题目录:http://blog.csdn.net/dba_huangzj ...

  10. Dynamics CRM EntityCollection 根据实体中的某个字段为依据去除重复数据

    CRM中通过QueryExpression查询出了一个EntityCollection集,但有时会存在重复数据,QueryExpression中有个属性distinct,只要设置为true就能过滤 ...