题目来源:

https://leetcode-cn.com/problems/valid-palindrome/

题目描述:

LeetCode125.验证回文串

解法描述:

 由于题目中说到只考虑字母和数字字符,可以忽略字母的大小写,因此我们需要先将空字符删除并且将字符变为全大写或者全小写。因此我们可以重新建一个数组存放处理后的数据,然后再双指针比较。

代码如下:

class Solution {
    public boolean isPalindrome(String s) {
        if (s == null || s.length() < 1) {
            return true;
        }
        char[] data = s.toCharArray();
        char[] temp = new char[data.length];
        int len = -1, pre = 0;
        for (char c : data) {
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
                temp[++len] = c;
                continue;
            }
            if (c >= 'A' && c <= 'Z') {
                temp[++len] = (char) (c + 32);
            }
        }
        while (pre < len) {
            if (temp[pre++] != temp[len--]) {
                return false;
            }
        }
        return true;
    }
}

 

相关文章:

  • 2021-09-01
  • 2021-10-14
  • 2021-05-16
  • 2022-03-02
  • 2021-08-27
  • 2021-11-05
猜你喜欢
  • 2021-08-13
  • 2021-08-06
  • 2022-12-23
  • 2021-07-20
  • 2021-08-25
相关资源
相似解决方案