一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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?

(二)解题

题目大意:给定一个非负数,对每一位进行相加,得到的和继续位相加,直到和为一位数为止

解题思路一

一开始,顺着题目的意思写下如下循环:

class Solution {
public:
    int addDigits(int num) {
        int ret = num;
        while(ret>=10)
        {
            int sum = 0;
            int temp = ret;
            while(temp){//每次计算每一位上的和
                sum+=temp%10;
                temp/=10;
            }
            ret = sum;
        }
        return ret;
    }
};

循环很简单,计算每一位的和,判断和是否为一位数,依次循环。

后来,突然看到题目中写了:Could you do it without any loop/recursion in O(1) runtime?

什么?O(1)时间!不用循环!下面看解法二的思路。

解题思路二

既然不用循环,就开始找规律,发现一直是1-9在循环,所以很容易想到mod9

要注意以下两个特殊情况:

(1) 0的时候为0

(2) mod9==0,此时返回9(除0外)

class Solution {
public:
    int addDigits(int num) {
        if(num==0) return 0;//特殊情况0
        return num%9==0?9:num%9;
    }
};

【一天一道LeetCode】#258. Add Digits的更多相关文章

  1. LN : leetcode 258 Add Digits

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

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

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  3. [LeetCode] 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

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  5. (easy)LeetCode 258.Add Digits

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

  6. Java [Leetcode 258]Add Digits

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

  7. LeetCode 258 Add Digits 解题报告

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

  8. leetcode 258. Add Digits(数论)

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

  9. LeetCode: 258 Add Digits(easy)

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

  10. leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇

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

随机推荐

  1. BookNote: Refactoring - Improving the Design of Existing Code

    BookNote: Refactoring - Improving the Design of Existing Code From "Refactoring - Improving the ...

  2. iphone inline video fragments

    DOMContentLoaded 它在DOM加载之后及资源加载之前被触发 通过递归调用同一方法来不断更新画面以达到动起来的效果,但它优于setTimeout/setInterval的地方在于它是由浏览 ...

  3. Tomcat的安装与配置

    Windows安装Tomcat与启动 安装版安装 安装版长这个样子,下载到电脑上后双击开始安装 双击后出现如下页面,点击 Next> 之后是这个页面,点击 I Agree 之后出现如下页面,点击 ...

  4. numpy的初探

    # data = numpy.genfromtxt("C:\\Users\\Admin\Desktop\\111.txt", delimiter='\t', dtype='str' ...

  5. vue2.0+ 从插件开发到npm发布

    vue: V2.5.11 此篇尽量详细,清楚的讲解vue插件的开发到npm的发布,想想将你自己做的东西展示给广大"网民",心里还是有点小激动的...-^_^ 先上一下插件效果图-- ...

  6. EXISTS的使用详解

    .exists的使用场合: exists 用于只能用于子查询,可以替代in,若匹配到结果,则退出内部 查询,并将条件标志为true,传回全部结果资料,in 不管匹配到匹配不到都 全部匹配完毕,使用ex ...

  7. os和sys模块的区别及其常用方法总结

    官方解释:os: This module provides a portable way of using operating system dependent functionality. 翻译:提 ...

  8. 如何恢复Initial commit之前的源文件

    在github新建了一个空的库,然后到本地文件夹下,git init了一下,将remote和本地的关联起来了,然后git pull了一下,本地的项目源码全没了,用以下命令可以帮你恢复 git refl ...

  9. 重载equals方法时要遵守的通用约定--自反性,对称性,传递性,一致性,非空性

    本文涉及到的概念 1.为什么重载equals方法时,要遵守通用约定 2.重载equals方法时,要遵守哪些通用约定   为什么重载equals方法时,要遵守通用约定 Object类的非final方法都 ...

  10. 学习笔记:Zookeeper 应用案例(上下线动态感知)

    1.Zookeeper 应用案例(上下线动态感知) 8.1 案例1--服务器上下线动态感知 8.1.1 需求描述 某分布式系统中,主节点可以有多台,可以动态上下线 任意一台客户端都能实时感知到主节点服 ...