Valid Palindrome

22 篇文章 0 订阅
6 篇文章 0 订阅
//palindrome回文
//我们只能考虑alphanumeric (字母与数字)。所以必须要找一个判别字母和数字的方法。int value=Integer.valueOf("1");//49
//所以我的想法用数组,存下有效的ASCII,做做比较
//
//还需注意ignoring cases.
public class Solution {
    public boolean isPalindrome(String s) {
        int len=s.length();
        if(len<=1)return true;//空集和一个元素都应该属于回文吧
        int[] a= new int[len];
        int j=0;
        for(int i=0;i<len;i++)
        {
            int Ascii=Integer.valueOf(s.charAt(i));
            if(Ascii>=65 && Ascii<=90)
            {
                a[j]=Ascii+32;
                j=j+1;
            }
            else if((Ascii>=48 && Ascii<=57)||(Ascii>=97 && Ascii<=122))
            {
                a[j]=Ascii;
                j=j+1;
            }
        }
        float middle = j;
        middle = ( middle-1)/2;//这里不能写成float middle=( j-1)/2,因为( j-1)/2运算是整型的,当j=2,结果不是0.5而是0
        int m=0;
        for(int l=0; l<=middle;l++)
        {
            if(a[l]!=a[j-1-l]) return false;
        }
        if(m==0)return true;
        return false;
    }
}
O(n) runtime, O(1) space:
//下面是leetcode上给出的solution
//The idea is simple, have two pointers – one a//t the head while the other one at the tail. Move them towards each other until they meet 
<span style="font-family: Arial, Helvetica, sans-serif;">//while skipping non-alphanumeric> characters.</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值