8000 refactor 233 · thamer-all/Leetcode@416442a · GitHub
[go: up one dir, main page]

Skip to content

Commit 416442a

Browse files
refactor 233
1 parent 972b0a3 commit 416442a

File tree

1 file changed

+13
-13
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-13
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 233. Number of Digit One
5+
*
46
* Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
57
68
For example:
79
Given n = 13,
810
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
911
10-
Hint:
11-
12-
Beware of overflow.
12+
Hint: Beware of overflow.
1313
*/
1414
public class _233 {
15-
16-
public int countDigitOne(int n) {
17-
int count = 0;
18-
for (long k = 1; k <= n; k *= 10) {
19-
long r = n / k;
20-
long m = n % k;
21-
// sum up the count of ones on every place k
22-
count += (r + 8) / 10 * k + (r % 10 == 1 ? m + 1 : 0);
15+
public static class Solution1 {
16+
public int countDigitOne(int n) {
17+
int count = 0;
18+
for (long k = 1; k <= n; k *= 10) {
19+
long r = n / k;
20+
long m = n % k;
21+
// sum up the count of ones on every place k
22+
count += (r + 8) / 10 * k + (r % 10 == 1 ? m + 1 : 0);
23+
}
24+
return count;
2325
}
24-
return count;
2526
}
26-
2727
}

0 commit comments

Comments
 (0)
0