一个返回应该停止整个程序吗?

问题描述

这是 leetcode 问题 9

    class Solution {
    public boolean ispalindrome(int x) {
       if(x<0 || (x!= 0 && x%10 == 0) ){
           return false;
       }
        int res = 0;
        while(x>res){
            res = res*10 + x % 10;
            if(x == res || x == res/10){
                return true;
            }
            x = x/10;
        }
        return false;
    }
}

这是正确的答案,但我有一个问题。

if(x == res || x == res/10){
            return true;
        } 

这部分,如果return true,while循环结束后会有很多true出来。然而,一个返回是否应该停止整个程序?

解决方法

您实际上是在尝试找到字符串的中点并查看 2 位数字是否匹配。

示例: 假设数字是 5225。迭代看起来像这样:

分辨率 x

  1. 5 5225
  2. 52 522 这里 res==x/10 -> 返回 true。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...