一天一道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. 树莓派超声波测距+蜂鸣器(c语言)

    前边我们已经详细的讲解了树莓派控制超声波模块测距(http://www.cnblogs.com/yuemo/p/8888342.html)和超声波控制蜂鸣器模块发声(http://www.cnblog ...

  2. 机器学习基础—集成学习Bagging 和 Boosting

    集成学习 就是不断的通过数据子集形成新的规则,然后将这些规则合并.bagging和boosting都属于集成学习.集成学习的核心思想是通过训练形成多个分类器,然后将这些分类器进行组合. 所以归结为(1 ...

  3. Cloud TPU Demos(TensorFlow 云 TPU 样例代码)

    Cloud TPU Demos 这是一个Python脚本的集合,适合在开源TensorFlow和 Cloud TPU 上运行. 如果您想对模型做出任何修改或改进,请提交一个 PR ! https:// ...

  4. tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例

    tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例 #!/usr/bin/env python # -*- coding: utf-8 ...

  5. iOS 搜索记录

    需求描述: 使用单独的搜索界面, 提供用户进行搜索并留下搜索记录. 搜索记录可以提供用户进行再次搜索, 或者把搜索记录清空. 方案和技术点: 存储方式使用 NSUserDefaults, 把对应的字段 ...

  6. 使用Aes对称加密解密Web.Config数据库连接串

    现在很多公司开始为了保证数据库的安全性,通常会对Web.Config的数据库连接字符串进行加密.本文将介绍学习使用Aes加密解密数据库连接字符串.本文采用MySql数据库. AES概念简述 AES 是 ...

  7. linux安装ftp

    安装vsftpd 1.以管理员(root)身份执行以下命令 1.  yum install vsftpd 2.设置开机启动vsftpd ftp服务 1.  chkconfig vsftpd on 3. ...

  8. 使用Fiddler改变线上js文件的引用路径

    一般的项目开发都是先在本地环境开发,测试环境中完成测试,最后再提交到线上环境. 但是由于版本构建工具有时出现bug或者一些缓存的因素导致测试环境代码可能和线上不一样,这是多么蓝瘦的事情.此处说的是在原 ...

  9. 搭建 RabbitMQ Server 高可用集群

    阅读目录: 准备工作 搭建 RabbitMQ Server 单机版 RabbitMQ Server 高可用集群相关概念 搭建 RabbitMQ Server 高可用集群 搭建 HAProxy 负载均衡 ...

  10. 如何去掉修改Joomla、joomlart及其模版版权、标志、图标的方法

    Joomla是遵循GNU通用公共授权(GPL)的自由软件,我们虽然不推荐将Joomla的所有版权删除,但有些必要的信息还是需要修改的,下面以JoomlArt.com 的JA_teline_iii_v2 ...