/**
* Problem: LeetCode 3871 - Count Commas in Range II
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public long countCommas(long n) {
long result = 0;
long threshold = 1000L;
while (threshold <= n) {
result += (n - threshold + 1);
threshold *= 1000L;
}
return result;
}
}
"""
Problem: LeetCode 3871 - Count Commas in Range II
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def countCommas(self, n: int) -> int:
result = 0
threshold = 1000
while threshold <= n:
result += (n - threshold + 1)
threshold *= 1000
return result
/**
* Problem: LeetCode 3871 - Count Commas in Range II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
long long countCommas(long long n) {
long long result = 0;
long long threshold = 1000LL;
while (threshold <= n) {
result += (n - threshold + 1);
if (threshold > n / 1000LL) {
break;
}
threshold *= 1000LL;
}
return result;
}
};
/**
* Problem: LeetCode 3871 - Count Commas in Range II
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var countCommas = function(n) {
let result = 0n;
const bigN = BigInt(n);
let threshold = 1000n;
while (threshold <= bigN) {
result += (bigN - threshold + 1n);
threshold *= 1000n;
}
return Number(result);
};