LeetCode //C - 233. Number of Digit One

233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
 

Example 1:

Input: n = 13
Output: 6

Example 2:

Input: n = 0
Output: 0

Constraints:
  • 0 < = n < = 1 0 9 0 <= n <= 10^9 0<=n<=109

From: LeetCode
Link: 233. Number of Digit One


Solution:

Ideas:

1. Initialization:

  • count keeps track of the total number of 1s.
  • factor is used to represent the current digit place we are evaluating (units, tens, hundreds, etc.).

2. Loop through each digit place:

  • lower_numbers: Numbers that are lower than the current digit place.
  • current_digit: The digit at the current place.
  • higher_numbers: Numbers that are higher than the current digit place.

3. Counting logic:

  • If current_digit is 0, the count of 1s is determined by the higher digits.
  • If current_digit is 1, we add the count from higher digits and the count from lower digits plus one (for the current digit itself).
  • If current_digit is greater than 1, we add the count from higher digits plus one entire set of the current digit place.

4. Increment the factor: Move to the next higher digit place by multiplying the factor by 10.

Code:
int countDigitOne(int n) {
    if (n <= 0) return 0;
    
    long long count = 0;
    long long factor = 1;
    
    while (factor <= n) {
        long long lower_numbers = n - (n / factor) * factor;
        long long current_digit = (n / factor) % 10;
        long long higher_numbers = n / (factor * 10);
        
        if (current_digit == 0) {
            count += higher_numbers * factor;
        } else if (current_digit == 1) {
            count += higher_numbers * factor + lower_numbers + 1;
        } else {
            count += (higher_numbers + 1) * factor;
        }
        
        factor *= 10;
    }
    
    return count;
}

相关推荐

  1. Python comp233

    2024-07-19 14:48:02       54 阅读
  2. UVA-213

    2024-07-19 14:48:02       52 阅读
  3. [LeetCode][233]数字 1 的个数

    2024-07-19 14:48:02       41 阅读
  4. LeetCode //C - 233. Number of Digit One

    2024-07-19 14:48:02       24 阅读
  5. 213. 打家劫舍 II

    2024-07-19 14:48:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 14:48:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 14:48:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 14:48:02       57 阅读
  4. Python语言-面向对象

    2024-07-19 14:48:02       68 阅读

热门阅读

  1. Redis详解

    2024-07-19 14:48:02       21 阅读
  2. PyTorch深度学习入门汇总

    2024-07-19 14:48:02       24 阅读
  3. 非结构化网格跨结点并行渲染

    2024-07-19 14:48:02       20 阅读
  4. 快速删除node_modules

    2024-07-19 14:48:02       19 阅读
  5. 小抄 20240714 我即世界

    2024-07-19 14:48:02       21 阅读
  6. 二叉树---找树左下角的值

    2024-07-19 14:48:02       18 阅读
  7. Android 一体机等root后的机器指令截屏

    2024-07-19 14:48:02       21 阅读
  8. 【题解】StarryCoding P211 勇闯高塔

    2024-07-19 14:48:02       22 阅读
  9. Linux 之 设置环境变量

    2024-07-19 14:48:02       24 阅读