233. Number of Digit One
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the total count of the digit '1' in all non-negative integers from 0 to n. A naive brute-force approach, iterating from 0 to n and counting the '1's in each number's decimal representation, would be too slow. Given that n can be up to 10^9, an iteration of this size would result in a Time Limit Exceeded (TLE) error. Therefore, a more efficient, mathematical approach is required. The key idea is to count the occurrences of '1' for each digit place (units, tens, hundreds, etc.) across the entire range [0, n] and then sum these counts.
Solution
Number of Digit One in Python
class Solution:
def countDigitOne(self, n: int) -> int:
# Python's integers handle arbitrary size, so no overflow issues.
k = 1
count = 0
# Iterate through each digit place: 1s, 10s, 100s, etc.
while k <= n:
# Split n into three parts based on the current digit place k:
# high: digits to the left of the current place.
# cur: the digit at the current place.
# low: digits to the right of the current place.
# Example: n = 234, k = 10 -> high = 2, cur = 3, low = 4.
high = n // (k * 10)
cur = (n // k) % 10
low = n % k
# Count the number of '1's at the current place 'k'.
# The logic depends on the value of the current digit 'cur'.
if cur == 0:
# If cur is 0, the number of 1s is determined by the higher digits.
# For each full block represented by 'high', there are 'k' ones.
# Example: n=204, k=10. high=2, cur=0.
# Tens digit is 1 for 10-19, 110-119. So high*k = 2*10 = 20.
count += high * k
elif cur == 1:
# If cur is 1, the count includes full blocks from 'high',
# plus the numbers in the current partial block.
# The partial block contributes 'low + 1' ones.
# Example: n=214, k=10. high=2, cur=1, low=4.
# Count is (high*k) for 0-199, plus (low+1) for 210-214.
# (2*10) + (4+1) = 25.
count += high * k + low + 1
else: # cur > 1
# If cur > 1, the count includes a full block for the current
# 'high' value as well. So (high + 1) full blocks.
# Example: n=234, k=10. high=2, cur=3.
# The tens digit '1' for the 200s range (210-219) is fully included.
# So, we count for high+1 blocks: (2+1)*10 = 30.
count += (high + 1) * k
k *= 10
return count
Number of Digit One in Java
class Solution {
public int countDigitOne(int n) {
// Use long for k to prevent overflow, as k can reach 10^9 and k*10 would overflow int.
long k = 1;
int count = 0;
// Iterate through each digit place: 1s, 10s, 100s, etc.
for (k = 1; k <= n; k *= 10) {
// Split n into three parts based on the current digit place k:
// high: digits to the left of the current place.
// cur: the digit at the current place.
// low: digits to the right of the current place.
// Example: n = 234, k = 10 -> high = 2, cur = 3, low = 4.
long high = n / (k * 10);
long cur = (n / k) % 10;
long low = n % k;
// Count the number of '1's at the current place 'k'.
// The logic depends on the value of the current digit 'cur'.
if (cur == 0) {
// If cur is 0, the number of 1s is determined by the higher digits.
// For each full block represented by 'high', there are 'k' ones.
// Example: n=204, k=10. high=2, cur=0.
// Tens digit is 1 for 10-19, 110-119. So high*k = 2*10 = 20.
count += high * k;
} else if (cur == 1) {
// If cur is 1, the count includes full blocks from 'high',
// plus the numbers in the current partial block.
// The partial block contributes 'low + 1' ones.
// Example: n=214, k=10. high=2, cur=1, low=4.
// Count is (high*k) for 0-199, plus (low+1) for 210-214.
// (2*10) + (4+1) = 25.
count += high * k + low + 1;
} else { // cur > 1
// If cur > 1, the count includes a full block for the current
// 'high' value as well. So (high + 1) full blocks.
// Example: n=234, k=10. high=2, cur=3.
// The tens digit '1' for the 200s range (210-219) is fully included.
// So, we count for high+1 blocks: (2+1)*10 = 30.
count += (high + 1) * k;
}
}
return count;
}
}
Number of Digit One in C++
class Solution {
public:
int countDigitOne(int n) {
// Use long long for k to prevent overflow, as k can reach 10^9 and k*10 would overflow int.
long long k = 1;
int count = 0;
// Iterate through each digit place: 1s, 10s, 100s, etc.
for (k = 1; k <= n; k *= 10) {
// Split n into three parts based on the current digit place k:
// high: digits to the left of the current place.
// cur: the digit at the current place.
// low: digits to the right of the current place.
// Example: n = 234, k = 10 -> high = 2, cur = 3, low = 4.
long long high = n / (k * 10);
long long cur = (n / k) % 10;
long long low = n % k;
// Count the number of '1's at the current place 'k'.
// The logic depends on the value of the current digit 'cur'.
if (cur == 0) {
// If cur is 0, the number of 1s is determined by the higher digits.
// For each full block represented by 'high', there are 'k' ones.
// Example: n=204, k=10. high=2, cur=0.
// Tens digit is 1 for 10-19, 110-119. So high*k = 2*10 = 20.
count += high * k;
} else if (cur == 1) {
// If cur is 1, the count includes full blocks from 'high',
// plus the numbers in the current partial block.
// The partial block contributes 'low + 1' ones.
// Example: n=214, k=10. high=2, cur=1, low=4.
// Count is (high*k) for 0-199, plus (low+1) for 210-214.
// (2*10) + (4+1) = 25.
count += high * k + low + 1;
} else { // cur > 1
// If cur > 1, the count includes a full block for the current
// 'high' value as well. So (high + 1) full blocks.
// Example: n=234, k=10. high=2, cur=3.
// The tens digit '1' for the 200s range (210-219) is fully included.
// So, we count for high+1 blocks: (2+1)*10 = 30.
count += (high + 1) * k;
}
}
return count;
}
};
Number of Digit One in TypeScript
function countDigitOne(n: number): number {
// JavaScript's number type is 64-bit float, safe for integers up to 2^53 - 1.
// This is sufficient for the problem's constraints.
let k = 1;
let count = 0;
// Iterate through each digit place: 1s, 10s, 100s, etc.
while (k <= n) {
// Split n into three parts based on the current digit place k:
// high: digits to the left of the current place.
// cur: the digit at the current place.
// low: digits to the right of the current place.
// Example: n = 234, k = 10 -> high = 2, cur = 3, low = 4.
const high = Math.floor(n / (k * 10));
const cur = Math.floor(n / k) % 10;
const low = n % k;
// Count the number of '1's at the current place 'k'.
// The logic depends on the value of the current digit 'cur'.
if (cur === 0) {
// If cur is 0, the number of 1s is determined by the higher digits.
// For each full block represented by 'high', there are 'k' ones.
// Example: n=204, k=10. high=2, cur=0.
// Tens digit is 1 for 10-19, 110-119. So high*k = 2*10 = 20.
count += high * k;
} else if (cur === 1) {
// If cur is 1, the count includes full blocks from 'high',
// plus the numbers in the current partial block.
// The partial block contributes 'low + 1' ones.
// Example: n=214, k=10. high=2, cur=1, low=4.
// Count is (high*k) for 0-199, plus (low+1) for 210-214.
// (2*10) + (4+1) = 25.
count += high * k + low + 1;
} else { // cur > 1
// If cur > 1, the count includes a full block for the current
// 'high' value as well. So (high + 1) full blocks.
// Example: n=234, k=10. high=2, cur=3.
// The tens digit '1' for the 200s range (210-219) is fully included.
// So, we count for high+1 blocks: (2+1)*10 = 30.
count += (high + 1) * k;
}
k *= 10;
}
return count;
}
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.