LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数

LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数

文章目录

  • LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数
  • 题目描述
  • 一、解题关键词
  • 二、解题报告
    • 1.思路分析
    • 2.时间复杂度
    • 3.代码示例
    • 2.知识点
  • 总结

题目描述


LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数
提示:

1 <= n < 2^31

一、解题关键词


二、解题报告

1.思路分析

2.时间复杂度

3.代码示例

class Solution {
    public int countDigitOne(int n) {
        int digit = 1 , res = 0;
        int high = n / 10,cur = n % 10, low = 0;
        while(high != 0 || cur != 0){
            if(cur == 0){res +=high * digit;}
            else if (cur == 1){ res += high * digit + low + 1;}
            else{ res +=(high + 1) * digit;}

            low += cur *digit;
            cur = high % 10;
            high /= 10;
            digit *= 10;
        }
        return res;

    }
}

2.知识点



总结

你可能感兴趣的:(LeetCode解题报告,java,算法,力扣)