题目:

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;
    }
}

最新文章

  1. 详解Linux目录(目录树详细解释)
  2. PMIC RTC 寄存器
  3. 关于MVC中模型model的验证问题
  4. android手势事件 快速移动 长按触摸屏 按下触摸屏,并拖动
  5. RPM命令用法详解
  6. svg 文字
  7. STM32定时器
  8. CSS 自动隐藏文字并添加省略号
  9. 团队作业4——第一次项目冲刺(Alpha版本)11.14
  10. Ubuntu如何使用Vscode写C++代码
  11. [翻译]一个新式的基于文本的浏览器 Browsh
  12. .net webapi跨域方法整理
  13. Unity 3D用简单的Cube、Sphere实现镜面/哈哈镜效果,只需十几秒哦!
  14. 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
  15. 升级到AndroidStudio3.2.1的注意事项
  16. Xcode7安装CocoaPods
  17. iOS 11 使用方法替换(Method Swizzling),去掉导航栏返回按钮的文字
  18. T-SQL备份数据库恢复
  19. django drf django-filter的method过滤
  20. node中express的中间件之methodOverride

热门文章

  1. C语言实现简单黑客帝国代码流
  2. CSDN帐号被封
  3. JQuery之事件处理
  4. Linux Debugging(八): core真的那么难以追踪吗?
  5. android EventBus 3.0使用指南
  6. 最简单的基于Flash的流媒体示例:RTMP推送和接收(ActionScript)
  7. SQL 2012 Always On 为 MSCRMSqlClrLogin SQL 登录名创建非对称密钥时报语法错误
  8. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal
  9. 使用VideoView实现简单视频播放器
  10. volatile实现可见性但不保证原子性