Java 请解释说明给定单词是否唯一的代码段?
有人能解释一下这个代码片段的工作原理吗?它告诉我们给定的单词是否唯一Java 请解释说明给定单词是否唯一的代码段?,java,Java,有人能解释一下这个代码片段的工作原理吗?它告诉我们给定的单词是否唯一 public static boolean isUniqueChars(String str) { if (str.length() > 128) { return false; } int checker = 0; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i) - '
public static boolean isUniqueChars(String str) {
if (str.length() > 128) {
return false;
}
int checker = 0;
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
if ((checker & (1 << val)) > 0) return false;
checker |= (1 << val);
}
return true;
}
public静态布尔值isUniqueChars(String str){
如果(str.length()>128){
返回false;
}
int-checker=0;
对于(int i=0;i checker |=(1checker的行为类似于布尔数组。此数组的每个元素都存储是否使用字符。
所以,checker |=(1要了解它在位级别是如何工作的,请放入Integer.tobinarysting()并进行检查。基本上,只要在任何整数值中使用逐位运算符,实际操作都会在相应的二进制值上进行
public static boolean isUniqueChars(String str) {
if (str.length() > 128) {
return false;
} int checker = 0;
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
System.out.println("Actual Value " + val); // ASCII substraction
System.out.println("Actual Value in Binary " + Integer.toBinaryString(val));
System.out.println("Left Shift Value in Binary " + Integer.toBinaryString(1<<val));
System.out.println("Operator Value is" + Integer.toBinaryString(checker & (1 << val))));
if ((checker & (1 << val)) > 0)
return false;
checker |= (1 << val); // checker = checker | (1<<val)
System.out.println("Checker Value in Binary " + Integer.toBinaryString(checker));
}
return true;
}
public静态布尔值isUniqueChars(String str){
如果(str.length()>128){
返回false;
}int-checker=0;
对于(int i=0;i System.out.println(“二进制中的左移位值”+Integer.toBinaryString(1)这是一个位掩码。谢谢,这到底是什么:“|=”?自从它声明为int以来,它的行为到底像一个数组吗?我想128检查应该是因为总共可能有128个唯一字符?
public static boolean isUniqueChars(String str) {
if (str.length() > 128) {
return false;
} int checker = 0;
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
System.out.println("Actual Value " + val); // ASCII substraction
System.out.println("Actual Value in Binary " + Integer.toBinaryString(val));
System.out.println("Left Shift Value in Binary " + Integer.toBinaryString(1<<val));
System.out.println("Operator Value is" + Integer.toBinaryString(checker & (1 << val))));
if ((checker & (1 << val)) > 0)
return false;
checker |= (1 << val); // checker = checker | (1<<val)
System.out.println("Checker Value in Binary " + Integer.toBinaryString(checker));
}
return true;
}