Leetcode 125. Valid Palindrome 回文验证 解题报告

来源:互联网 发布:阿里巴巴国际淘宝网 编辑:程序博客网 时间:2024/05/18 03:38

1 解题思想

就一个简单的判断一个string里面的字符和数字部分,是否是回文的,不考虑符号和大小写等

其实说白了,就首尾指针对比一下就好。。 我代码里面首尾不同的地方时过滤下非字符数字

2 原题

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that the string might be empty? This is a good question to ask during an interview.For the purpose of this problem, we define empty string as valid palindrome

3 AC解

public class Solution {    /**     * 两个指针,注意只考虑数字和字母,而且不考虑大小写     * */    public boolean isPalindrome(String s) {        int start=0,end=s.length()-1;        s=s.toLowerCase();        while(start<end){            while(start<end && !(s.charAt(start)>='a' && s.charAt(start)<='z')  && !(s.charAt(start)>='0' && s.charAt(start)<='9')) start++;            while(start<end && !(s.charAt(end)>='a' && s.charAt(end)<='z')  && !(s.charAt(end)>='0' && s.charAt(end)<='9')) end--;            if(start>=end)                break;            if(s.charAt(start)!=s.charAt(end)) return false;            start++;            end--;        }        return true;    }}

最近频繁发生因为leetcode题目测试数据变动而造成的代码不能AC,所以这里特别贴出AC
因为代码完成时间长短不定,所以如果我给的代码不能AC了,请及时评论通知我,我会及时修改的
这里写图片描述

0 0