๐๐๐ 233. ๆฐๅญ 1 ็ไธชๆฐ
2022ๅนด10ๆ10ๆฅ
- algorithm
๐๐๐ 233. ๆฐๅญ 1 ็ไธชๆฐ
้พๅบฆ: ๐๐๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
public int countDigitOne(int n) {
// ๆ่ทฏ๏ผ
// ไพๆฌก่ฎก็ฎๆฏไธช้้ถไฝไธญ 1 ็ไธชๆฐ๏ผๅๅณไบ ๅฝๅไฝ็ๅผ & ้ซไฝๅผ & ไฝไฝ
// ๅบๅฎ่ฏฅไฝ๏ผๅ
ถไฝไฝๆๅคๅฐ็งๆ
ๅต
// ไพ๏ผ6017
// ไธชไฝ 7 -> 602 * 1
// ๅไฝ 1 -> 60 * 10 + 8
// ็พไฝ 0 -> 6 * 100
// ๅไฝ 6 -> 1 * 1000
int res = 0;
int high = n / 10;
int cur = n % 10;
int low = 0;
int mul = 1;
while(high != 0 || cur != 0) {
// System.out.println(high + " " + mul + " " + low + " " + cur);
if(cur == 0) {
// ้ซไฝ * mul
res += high * mul;
} else if(cur == 1) {
// ้ซไฝ * mul + ไฝไฝ + 1
res += high * mul + low + 1;
} else {
// cur > 1
res += (high + 1) * mul;
}
low += cur * mul;
mul *= 10;
cur = high % 10;
high /= 10;
}
return res;
}
}