有没有办法可以在速度方面改进这种方法?
static int booleanArrayToInt(boolean[] array) {
int x = 0;
int i = 0;
for (boolean b : array) {
i++;
if (b) {
x = 1;
break;
}
}
for (int j = i; j < array.length; j++) {
if (array[j]) x = (x << 1) + 1;
else x = x << 1;
}
return x;
}
发布于 2019-06-26 07:51:57
这就是你要的。
boolean[] bools =
{ true, true, false, false, true, true, false, false, true
};
public static int binaryToInt(boolean[] bools) {
int x = 0;
for (boolean b : bools) {
x <<= 1;
x |= b ? 1
: 0;
}
return x;
}
https://stackoverflow.com/questions/56763230
复制相似问题