标题几乎是不言自明的。:)
1232 => 0
1231030 => 1
2000 => 3
34444400000 => 5发布于 2014-09-10 19:23:44
如果它适合于int/long,只需检查数字模10是否为0,并保留一个计数器:
long x = ...
if (x == 0) {
return 0;
}
int counter = 0;
while (x % 10 == 0) {
counter++;
x /= 10;
}如果它太大,无法适应long,那么将其存储在一个String中,并从最后一个字符计数零:
String s = ...
int counter = 0;
while(counter < s.length() && s.charAt(s.length() - 1 - counter) == '0') {
counter++;
}发布于 2021-04-23 07:40:15
Integer类有一个内置函数来计数尾随零。javadocs
int trailingZeroes = Integer.numberOfTrailingZeros(int i);发布于 2014-09-10 19:23:16
三行:
int zeroes = 0
while(num%10 == 0 && num != 0) {
zeroes++;
num /= 10;
}这使用了模算子。只要我们可以除以没有剩余的十,就增加计数器。
https://stackoverflow.com/questions/25773422
复制相似问题