304 North Cardinal St.
Dorchester Center, MA 02124

# Number of Digit One LeetCode Solution

## Problem – Number of Digit One LeetCode Solution

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 <= 109`

## Number of Digit One LeetCode Solution in C++

``````int countDigitOne(int n) {
int ones = 0;
for (long long m = 1; m <= n; m *= 10)
ones += (n/m + 8) / 10 * m + (n/m % 10 == 1) * (n%m + 1);
return ones;
}
``````

## Number of Digit One LeetCode Solution in Java

``````public int countDigitOne(int n) {
int ones = 0;
for (long m = 1; m <= n; m *= 10)
ones += (n/m + 8) / 10 * m + (n/m % 10 == 1 ? n%m + 1 : 0);
return ones;
}
``````

## Number of Digit One LeetCode Solution in Python

``````def countDigitOne(self, n):
ones, m = 0, 1
while m <= n:
ones += (n/m + 8) / 10 * m + (n/m % 10 == 1) * (n%m + 1)
m *= 10
return ones
``````
##### Number of Digit One LeetCode Solution Review:

In our experience, we suggest you solve this Number of Digit One LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Number of Digit One LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Number of Digit One LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions