public static boolean isUnique(String astr) {
long low64 = 0;
long high64 = 0;
for (char c : astr.toCharArray()) {//字符串转成字符数组
if (c >= 64) {
long bitIndex = 1L << (c - 64);//1L-字母的ascii码(数字小于64,大写字母65起,小写字母97起)
if ((high64 & bitIndex) != 0) {//完全相同的两个就是0,& 把字符转成二进制,有一个是0就是0
return false;
}
high64 |= bitIndex;
} else {
long bitIndex = 1L << c;
if ((low64 & bitIndex) != 0) {
return false;
}
low64 |= bitIndex;
}
}
return true;
}
如何判断字符串是否有重复字符
最新推荐文章于 2022-10-09 22:49:35 发布