leetcode 9。

发布于 2025-02-08 06:56:09 字数 1034 浏览 2 评论 0原文

我正在尝试将整数转换为字符串。 在子句中,在中,如果我使用s.charat(i)== s.charat(j)首先,然后i ++j -,我无法通过输入= 121的测试,我得到了错误而不是true。但是,如果我将s.charat(i)!= s.charat(j)首先放置,然后接受。

错误答案:

class Solution {
    public boolean isPalindrome(int x) {
        String s = String.valueOf(x); //n
        int l = s.length();
        int i = 0;
        int j = l - 1;
        while (i <= j) {
            if (s.charAt(i) == s.charAt(j)) {
                i++;
                j--;
            }
            return false;
        }
        return true;
    }

接受答案:

class Solution {
    public boolean isPalindrome(int x) {
        String s = String.valueOf(x); //n
        int l = s.length();
        int i = 0;
        int j = l - 1;
        while (i <= j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

我想念什么?

I am trying to do this converting the integer to a string.
Inside the if clause, if I used s.charAt(i) == s.charAt(j) first then i++, j--, I couldn't pass the test of input = 121, I got false instead of true. But if I put s.charAt(i) != s.charAt(j) first, then accepted.

Wrong answer:

class Solution {
    public boolean isPalindrome(int x) {
        String s = String.valueOf(x); //n
        int l = s.length();
        int i = 0;
        int j = l - 1;
        while (i <= j) {
            if (s.charAt(i) == s.charAt(j)) {
                i++;
                j--;
            }
            return false;
        }
        return true;
    }

Accepted answer:

class Solution {
    public boolean isPalindrome(int x) {
        String s = String.valueOf(x); //n
        int l = s.length();
        int i = 0;
        int j = l - 1;
        while (i <= j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

What did I miss?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2025-02-15 06:56:09

错误的解决方案返回false在任何情况下,正确的解决方案仅返回false s.charat(i)!= s.charat(j)是真的。

如果要跳过第一个代码中的false; 返回false; ,则您要么需要将其移至else条款中,或者使用继续; 在如果子句直接跳到下一个循环迭代。

The wrong solution returns false in any case and the correct solution only returns false when s.charAt(i) != s.charAt(j) is true.

If you want to skip return false; in the first code, you either need to move it into an else clause or use continue; in the if clause in order to directly jump to the next loop iteration.

你另情深 2025-02-15 06:56:09

为了解决回文,更容易逆转字符串并将其与原件进行比较。

public boolean isPalindrome(int x) {
    StringBuffer palindrome = new StringBuffer();
    palindrome.append(x);

    String s1 = palindrome.toString();
    String s2 = palindrome.reverse().toString();

    return s1.equals(s2);
}

To solve a palindrome, it's easier to reverse the string and compare it with the original.

public boolean isPalindrome(int x) {
    StringBuffer palindrome = new StringBuffer();
    palindrome.append(x);

    String s1 = palindrome.toString();
    String s2 = palindrome.reverse().toString();

    return s1.equals(s2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文