一天一道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. Linux命令-关机命令详解

    关机命令:1.halt 立刻关机 2.poweroff 立刻关机 3.shutdown -h now 立刻关机(root用户使用) 4.shutdown -h 10 10分钟后自动关机 如果是通过sh ...

  2. xadmin库的下载安装及奇葩报错的解决方法

    今天主要讲xadmin库的下载和安装的.......各种问题....... 先注明:我使用的是python3.6,Django2.0,所以xadmin也应该是2.0版本会比较适配. 所以这里先给个xa ...

  3. Mysql锁机制--写锁

    Mysql 系列文章主页 =============== 1 准备数据 1.1 建表 1.1.1 建立 Employee表 DROP TABLE IF EXISTS employee; CREATE ...

  4. IntelliJ IDEA光标变粗 backspace无法删除内容解决方法

    进入了vim插件 1.ctrl+alt+s快捷键打开Settings 2.选择左侧列表中的Plugins 3.在右侧面板的搜索框中搜索IdeaVim 4.将复选框中的钩子去掉 backspace成了其 ...

  5. webpack require.ensure 按需加载

    使用 vue-cli构建的项目,在 默认情况下 ,会将所有的js代码打包为一个整体比如index.js.当使用存在多个路由代码的时候,index.js可能会超大,影响加载速度. 这个每个路由页面除了i ...

  6. mongo数据更新(修改器)

    数据更新简单的做法是删除重新插入update()函数语法 db.集合.update(更新条件,新的对象数据(更新操作符),upsert,multi)upsert如果要更新的数据不存在,则增加一条新的内 ...

  7. easyui datagrid属性和方法

    本文可以当做api来使用 使用方法(Usage Example) 从现有的表单元素创建数据表格,定义在html中的行,列和数据. <table class="easyui-datagr ...

  8. url重定向或者重写

    有四种方式:1.urlMappings,返回200状态码 <system.web> <urlMappings > <add url="~/others.aspx ...

  9. 使用ajax上传图片,支持图片即时浏览,支持js图片压缩后上传给服务器

    使用ajax上传图片,支持图片即时浏览,支持js图片压缩后上传给服务器 ajax上传主要使用了 var reader = new FileReader() 此方法 js图片压缩主要是利用canvas进 ...

  10. ucsc 文件格式说明

    链接附带的是ucsc各种格式文件的说明,以后遇到新类型的文件可以先来这里看看! https://www.genome.ucsc.edu/FAQ/FAQformat.html