题目:

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.

思路:

  • 题意:判断一个字符串是不是回文
  • 这道题可以采用双指针,开头first,结尾sed,first++,sed–,first < sed
  • 要求不考虑大小写,全部转化为大写,同时判断字符是不是字母和数字,‘A’,‘Z’,‘0’,‘9’

代码:

public class Solution {
    public boolean isPalindrome(String s) {
        if(s == null){
            return true;
        }
        char A = 'A';
        char Z = 'Z';
        char numMin = '0';
        char numMax = '9';
        s = s.toUpperCase();
        int first  = 0;
        int sed = s.length() - 1;
        while(first < sed){
            if((s.charAt(first) < A||s.charAt(first) > Z) && (s.charAt(first) < numMin||s.charAt(first) > numMax)){
                first++;
                continue;
            }
            if((s.charAt(sed) < A||s.charAt(sed) > Z) && (s.charAt(sed) < numMin||s.charAt(sed) > numMax)){
                sed--;
                continue;
            }
            if(s.charAt(first) == s.charAt(sed)){
                first++;
                sed--;
            }else{
                return false;
            }
        }
        return true;
    }
}

LeetCode(61)-Valid Palindrome的更多相关文章

  1. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  2. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. [leetcode] 1. Valid Palindrome

    leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...

  4. LeetCode 1216. Valid Palindrome III

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...

  5. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  7. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. leetcode 125. Valid Palindrome ----- java

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

随机推荐

  1. LATEX TEMPLATE (SPRINGER) (*.BST)

    该模板在哪里下载? http://www.springer.com/computer/image+processing/journal/11263, Instructions for Authors, ...

  2. Android简易实战教程--第八话《短信备份~一》

    各种手机助手里面都包含了短信备份这一项.短信的本分主要包含四项:内容body.事件date.方式type.号码address. 短信备份~一.使用一种很笨的方式来保存短信到xml文件中,而且保存在外部 ...

  3. UNIX网络编程——关于socket阻塞与非阻塞情况下的recv、send、read、write返回值

    1.阻塞模式与非阻塞模式下recv的返回值各代表什么意思?有没有 区别?(就我目前了解阻塞与非阻塞recv返回值没有区分,都是 <0:出错,=0:连接关闭,>0接收到数据大小,特别:返回 ...

  4. iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...

  5. 手把手教你轻松实现listview下拉刷新

    很多人觉得自定义一个listview下拉刷新上拉加载更多是一件很牛x的事情,不是大神写不出来,我想大多数童鞋都是做项目用到时就百度,什么pulltorefresh,xlistview...也不看原理, ...

  6. Beanstalkd 一个高性能分布式内存队列系统

    需要一个分布式内存队列,能支持这些特性:任务不重不漏的分发给消费者(最基础的).分布式多点部署.任务持久化.批量处理.错误重试..... 转载:http://rdc.taobao.com/blog/c ...

  7. python类:描述器Descriptors和元类MetaClasses

    http://blog.csdn.net/pipisorry/article/details/50444769 描述器(Descriptors) 描述器决定了对象属性是如何被访问的.描述器的作用是定制 ...

  8. 【unix网络编程第三版】阅读笔记(二):套接字编程简介

    unp第二章主要将了TCP和UDP的简介,这些在<TCP/IP详解>和<计算机网络>等书中有很多细致的讲解,可以参考本人的这篇博客[计算机网络 第五版]阅读笔记之五:运输层,这 ...

  9. Java:将字符串中的数字转换成整型

    在C语言中,将字符串中的数字转换为整型的方法是是利用atoi这个函数.在Java中,我们可以利用parseInt方法来实现,具体代码如下: public class HelloWorld { publ ...

  10. pig运行方法:本地与云上

    pig脚本 放在本地当前目录(键入pig命令时,所处的目录),然后用进入grunt,用run或者exec调用 1云运行: 键入pig进入grunt,用run命令运行当前目录脚本.(或者外部用pig - ...