zoukankan      html  css  js  c++  java
  • 258. Add Digits 数位相加到只剩一位数

    [抄题]:

    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 = 111 + 1 = 2. Since 2 has only one digit, return it.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [一句话思路]:

    %9

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    if + else if不是完整的,if + else才是完整的

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    if + else if不是完整的,if + else才是完整的

    [复杂度]:Time complexity: O(1) Space complexity: O(1)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public int addDigits(int num) {
            if (num == 0) 
                return 0;
            
           if (num % 9 == 0) {
               return 9;
           }
            
           else{
               return num % 9;
           }
        }
    }
    View Code
  • 相关阅读:
    战胜忧虑<2>——忙碌可以消除忧虑
    战胜忧虑<1>——不要让忧郁侵入你的生活
    Django的下载和安装
    Github 如何上传本地文件
    Python_相对路径的获取
    Python_生成HTMLTestRunner测试报告
    Python_requests实例
    Charles抓包(Http/Https请求)
    Python_base_函数返回值
    Python_base_print 取消自动换行
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8976509.html
Copyright © 2011-2022 走看看