此代码适用于测试一个简单的回文,但我对它是如何工作感到困惑。
我对while循环条件感到困惑,检查if (left < right)
以及它是如何自动意味着它不是回文。
left = 0;
right = i.length() -1;
while(i.charAt(left) == i.charAt(right) && right > left){
left++;
right--;
}
System.out.println();
if (left < right)
System.out.println ("That string is Not a palindrome.");
else
System.out.println("That string IS a palindrome");
答案 0 :(得分:2)
while循环基本上同时从相对侧开始遍历潜在的回文,并比较每一侧的字符。它检查是否(右>左)因为如果是,则意味着'右'和'左'计数器没有相互传递,因此没有到达(或传递)字符串的中间。 在此检查之后,它向右递减(因此它会更接近字符串的中心)并递增“左”(出于同样的原因)。
最后,如果'left'仍然小于'right',则意味着循环在'left'或'right'到达字符串中间之前停止,并且因为字符的条件发生了在相反的索引上匹配在某个阶段是错误的。
如果他们确实到达(或通过)中间,'left'将是&gt; ='right',这意味着字符匹配条件至少是字符串的中间,因此是回文。
答案 1 :(得分:1)
Palindrome测试仅在您的字符串全部为大写或小写时才有效。
当左字符不等于右字符或右索引&gt;时,while循环将结束。左侧索引(您已经传递了字符串的“中间”。
if(left&lt; right)与right&gt;相同左边和字符串是一个回文(一个案例),否则左边的字符不等于正确的字符。
我希望这有用。
答案 2 :(得分:1)
while语句中的条件检查X == Y
和X < Y
。如果这些条件不再有效,它会在while循环中断开。
考虑以下回文。
迭代1:
X Y
+ +
| |
+-v--+----+---+---+---+---+---+---+-v-+
| | | | | | | | | |
| A | B | C | D | E | D | C | B | A |
+----+----+---+---+---+---+---+---+---+
迭代2:
+----+----+---+---+---+---+---+---+---+
| | | | | | | | | |
| A | B | C | D | E | D | C | B | A |
| | | | | | | | | |
+----+----+---+---+---+---+---+---+---+
^ ^
| |
+ +
X Y
迭代3:
X Y
+ +
| |
v v
+----+----+---+---+---+---+---+---+---+
| | | | | | | | | |
| A | B | C | D | E | D | C | B | A |
| | | | | | | | | |
+----+----+---+---+---+---+---+---+---+
迭代4:
X Y
+ +
| |
+- --+----+---+-v-+---+-v-+---+---+- -+
| | | | | | | | | |
| A | B | C | D | E | D | C | B | A |
+----+----+---+---+---+---+---+---+---+
迭代5:此时循环会中断。由于X < Y
不再有效
XY
++
||
+----+----+---+---+vv-+---+---+---+---+
| | | | | | | | | |
| A | B | C | D | E | D | C | B | A |
| | | | | | | | | |
+----+----+---+---+---+---+---+---+---+
如果以上示例不是回文并且看起来像这样
+- --+----+---+-v-+---+-v-+---+---+- -+
| | | | | | | | | |
| A | B | K | D | E | D | C | B | A |
+----+----+---+---+---+---+---+---+---+
然后你的while循环在第3次迭代时中断,因为你的第一个条件X==Y
无效。此时X
仍将小于Y
。因此,如果一个字符串不是回文,而循环肯定会在X
和{}之前中断。 Y
符合即X < Y
答案 3 :(得分:0)
使用print语句可以观察程序在运行时正在做什么。
class Ideone
{
private static void checkPalindrome(String i) {
int left = 0;
int right = i.length() -1;
System.out.println("This word is: " + i);
System.out.println("Checking charAt(" + left + ") which is " +
i.charAt(left) + " and chartAt(" + right + ") which is " +
i.charAt(right));
while(i.charAt(left) == i.charAt(right) && right > left) {
left++; right--;
System.out.println("Checking charAt(" + left + ") which is " +
i.charAt(left) + " and chartAt(" + right + ") which is " +
i.charAt(right));
}
System.out.println();
if (left < right)
System.out.println ("That string is Not a palindrome.");
else
System.out.println("That string IS a palindrome");
System.out.println();
}
public static void main (String[] args) throws java.lang.Exception
{
checkPalindrome("racecar");
checkPalindrome("abba");
checkPalindrome("java");
}
}
This word is: racecar
Checking charAt(0) which is r and chartAt(6) which is r
Checking charAt(1) which is a and chartAt(5) which is a
Checking charAt(2) which is c and chartAt(4) which is c
Checking charAt(3) which is e and chartAt(3) which is e
That string IS a palindrome
This word is: abba
Checking charAt(0) which is a and chartAt(3) which is a
Checking charAt(1) which is b and chartAt(2) which is b
Checking charAt(2) which is b and chartAt(1) which is b
That string IS a palindrome
This word is: java
Checking charAt(0) which is j and chartAt(3) which is a
That string is Not a palindrome.
正如您所看到的,while循环从字符串的远端开始,并比较每一侧的字符;如果它们相同,则每侧移动一个字符。它会检查right > left
,以便知道何时停止。一旦到达中间,right
将至少相等,如果不大于left
,因为它们在中间相遇并且朝向相反的方向。
left < right
的布尔否定等同于left >= right
。如果两人在中间相遇,那将是真的。如果left < right
那么他们就不能在中间相遇。您应该能够在上面的输出中看到这一点。
Run this code on ideone并再玩一遍。