Hard
You are given two positive integers left
and right
with left <= right
. Calculate the product of all integers in the inclusive range [left, right]
.
Since the product may be very large, you will abbreviate it following these steps:
C
.
3
trailing zeros in 1000
, and there are 0
trailing zeros in 546
.d
. If d > 10
, then express the product as <pre>...<suf>
where <pre>
denotes the first 5
digits of the product, and <suf>
denotes the last 5
digits of the product after removing all trailing zeros. If d <= 10
, we keep it unchanged.
1234567654321
as 12345...54321
, but 1234567
is represented as 1234567
."<pre>...<suf>eC"
.
12345678987600000
will be represented as "12345...89876e5"
.Return a string denoting the abbreviated product of all integers in the inclusive range [left, right]
.
Example 1:
Input: left = 1, right = 4
Output: “24e0”
Explanation: The product is 1 × 2 × 3 × 4 = 24.
There are no trailing zeros, so 24 remains the same.
The abbreviation will end with “e0”. Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
Thus, the final representation is “24e0”.
Example 2:
Input: left = 2, right = 11
Output: “399168e2”
Explanation: The product is 39916800.
There are 2 trailing zeros, which we remove to get 399168.
The abbreviation will end with “e2”. The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
Hence, the abbreviated product is “399168e2”.
Example 3:
Input: left = 371, right = 375
Output: “7219856259e3”
Explanation: The product is 7219856259000.
Constraints:
1 <= left <= right <= 104
class Solution {
fun abbreviateProduct(left: Int, right: Int): String {
val threshold0 = 100000000000000L
val threshold1 = 10000000000L
val threshold2: Long = 100000
var curr: Long = 1
var i: Int
var zerosCount = 0
i = left
while (i <= right && curr < threshold0) {
curr *= i.toLong()
while (curr % 10 == 0L) {
curr /= 10
zerosCount++
}
i++
}
if (curr < threshold1) {
return String.format("%de%d", curr, zerosCount)
}
var low = curr % threshold1
var high = curr.toDouble()
while (high > threshold1) {
high /= 10.0
}
while (i <= right) {
low *= i.toLong()
high *= i.toDouble()
while (low % 10 == 0L) {
low /= 10
zerosCount++
}
if (low >= threshold1) {
low %= threshold1
}
while (high > threshold1) {
high /= 10.0
}
i++
}
while (high >= threshold2) {
high /= 10.0
}
low %= threshold2
return String.format("%d...%05de%d", high.toInt(), low, zerosCount)
}
}