Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Number of Digit One problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Number of Digit One problem solution, we have given an integer n, and count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

Leetcode Number of Digit One problem solution

Leetcode Number of Digit One problem solution in Python.

class Solution:
    
    def countDigitOne(self, n): # can not deal with n=-1 case!
        if n<=0: return 0
        
        iCount = 0
        iFactor = 1
        iLowerNum, iCurNum, iHigherNum = 0, 0, 0
        
        while n // iFactor != 0:
            iLowerNum = n - (n//iFactor) * iFactor
            iCurNum = (n//iFactor) % 10
            iHigherNum = n //(iFactor * 10)
            
            if iCurNum == 0:
                iCount += iHigherNum * iFactor
            elif iCurNum == 1:
                iCount += iHigherNum * iFactor + (iLowerNum + 1)
            else:
                iCount += (iHigherNum + 1) * iFactor
            iFactor *= 10
        return iCount

Number of Digit One problem solution in Java.

public int countDigitOne(int n) {
       int res = 0;
       long a = 0;
       long b = 0;
       for(long m=1;m<=n;m*=10){
           a = n/m;
           b = n%m;
           if(a % 10 > 1){
               res += a/10 * m + m;
           }else if( a%10 == 1){
               res += a/10 * m + b + 1;
           }else{
               res += a/10 * m;
           }
       }
        return res;
    }

Problem solution in C++.

class Solution {
public:
    int countDigitOne(int n) {
        if(n <= 0)
            return 0;
        int  x = n;
        int y = 0;
        int res = 1;
        
        while(x >= 10)
        {
            res *= 10;
            x /= 10;
            ++y;
        }
        if(x == 1)
            return res / 10 * y +  n % res + 1 + countDigitOne(n % res);
        else
            return res / 10 * (n/res) * y +  res + countDigitOne(n % res);
         
    }
};

Problem solution in C.

int countDigitOne(int n){
    int temp = n, count = 0, rest = 0, coefficient = 0;
    long int step = 1;
    
    for (int i = 0; temp > 0; i++) {
        int digit = temp % 10;
        coefficient = step / 10 * i;
        if (digit > 1) {
            count += digit * coefficient + step;
        } else if (digit == 1) {
            count += coefficient + rest + 1;
        }
        rest += digit * step;
        step *= 10;
        temp /= 10;
    }
    return count;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes