while循环条件,不等于和大于之间的区别

huangapple go评论92阅读模式
英文:

while loop condtion, difference between not equal and greater than

问题

以下是您提供的内容的翻译部分:

我正在解决一个问题即判断一个整数是否为回文数我的逻辑是将其转换为字符串然后设置第一个和最后一个索引我循环遍历每个第一个和最后一个索引如果它们是回文数则值应该匹配在循环中我将第一个索引增加一将最后一个索引减少一所以最终它们会在中间相遇我最初的循环条件是如果两者的索引不相同但是这会导致字符串索引超出界限异常但是当我将循环条件更改为只要起始小于结束就可以时它就可以工作了

public boolean isPalindrome(int x) {

    if(x < 0) {
        return false;
    }    	
    String val = String.valueOf(x);
    
    int end = val.length()-1;
    int start = 0;
    while(start != end) {
        if(val.charAt(start) != val.charAt(end)) {
            return false;
        }
        start++;
        end--;
    }
    
    return true;
}
}

当我执行以下操作时它可以工作

if(str.charAt(start++) < str.charAt(end--)) return false;
英文:

I'm doing a problem checking whether an integer is a palindrome. The logic is I convert it to string and then set the index of the first and the last. I loop through each first and last index and the values should match if they are palindrome. in the loop I increase the first index by one, I decrease the last index by one. So eventually they will meet in the half. The loop condition I originally had was if the indexes of both are not the same. but this gives me String index out of bounds exception. But then when I changed it to loop through as long as start is smaller than the end, it works.

public boolean isPalindrome(int x) {

    	if(x &lt; 0) {
    		return false;
    	}    	
    	String val = String.valueOf(x);
    	
    	int end = val.length()-1;
    	int start = 0;
    	while(start != end) {
    		if(val.charAt(start) != val.charAt(end)) {
    			return false;
    		}
    		start++;
    		end--;
    	}
    	
    	return true;
    }
    }

it works when I do

 if(str.charAt(start++) &lt; str.charAt(end--)) return false;

答案1

得分: 3

如果长度是偶数,就会出现问题。例如,如果长度是4,你会得到以下运行结果:

开始 | 结束
0     3
1     2
2     1
3     0
4     -1 <--- 错误

因为在整个过程中,不等式条件始终为真。
英文:

You have problem if the length is even number. For example if length is 4, you will have this run:

START | END
0     3
1     2
2     1
3     0
4     -1 &lt;--- error

as during whole time the condition of not-equality is true.

答案2

得分: 0

问题是由于数字中的数字个数:

例如,对于 1221startend 永远不会在中间相遇:

  • 迭代 1:start = 0,end = 3
  • 迭代 2:start = 1,end = 2
  • 迭代 3:start = 2,end = 1
    ...

事实上,当你执行以下操作时:“它起作用”

if(str.charAt(start++) < str.charAt(end--)) return false;

纯粹是偶然。

你应该只需将 while 条件更改为:

while(start < end) {
英文:

The issue is due to the number of digit in the number :

for example, for 1221, start and end will never meet in the middle :

  • iteration 1 : start = 0, end = 3
  • iteration 2 : start = 1, end = 2
  • iteration 3 : start = 2, end = 1
    ...

The fact that "it works" when you do :

 if(str.charAt(start++) &lt; str.charAt(end--)) return false;

is by chance.

You should just change the while condition to :

while(start &lt; end) {

huangapple
  • 本文由 发表于 2020年5月31日 04:10:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/62108101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定