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.


题解:Using two pointers head and tail, head keeps moving forward till it points to an alpha or number; tail moves backward till it points to an alpha or number, then judge if what head points to equals what tail points, if it doesn't equal, return false; Else cotinue moving head and tail to compare characters bewteen them.

We also need a function isValid(char c) to judge if char c is a number or alpha.

The code is below:

 public class Solution {
private boolean isValid(char c){
if(c >= 'a' && c <= 'z')
return true;
if(c >= '0' && c <='9')
return true; return false;
} public boolean isPalindrome(String s) {
s = s.toLowerCase(); if(s == null || s.length() <= 1)
return true; int head = 0;
int tail = s.length() - 1; while(head < tail){
while(head < tail && !isValid(s.charAt(head)))
head++;
while(tail > head && !isValid(s.charAt(tail)))
tail--;
if(s.charAt(head) != s.charAt(tail))
return false;
else {
head++;
tail--;
} } return true;
}
}

【leetcode刷题笔记】Valid Palindrome的更多相关文章

  1. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. 【leetcode刷题笔记】Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  3. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  4. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  7. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. 【leetcode刷题笔记】Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. LeetCode 刷题笔记 2. 有效的括号(Valid Parentheses)

    tag: 栈(stack) 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须 ...

随机推荐

  1. Win7下IIS的安装与配置

    win7下IIS的安装和配置 图文教程,需要的朋友可以参考下 一.首先是安装IIS.打开控制面板,找到“程序与功能”,点进去 二.点击左侧“打开或关闭Windows功能” 三.找到“Internet ...

  2. github 答题

    头脑王者 / 百万英雄 / 冲顶大会 / 芝士超人 自动答题:https://github.com/cxs1994/python_answer 头脑王者:https://github.com/sear ...

  3. iptables基础及samba配置举例

    iptable基本概念 iptables防火墙包含两部分,即位于用户空间的iptables模块和位于内核空间netfilter模块.用户空间模块提供插入.修改和除去包过滤表中规则,内核模块进行实际的过 ...

  4. 博弈SG函数

    转自:Sprague-Grundy Function-SG函数--博弈论(3) 公平游戏的Sprague-Grundy定理 公平游戏是一种双人游戏,在游戏中双方都有完整的信息,没有牵涉,任何状态的合法 ...

  5. python解释器安装教程

    1. 首先,打开python的官网:python.org 2. 首页downloads下打开, 3. 最上边是两个最新的版本,长期计划,推荐使用python3,如果长期打算用p3,默认使用最新版本.如 ...

  6. 还在用 kill -9 停机?这才是最优雅的姿势(转)

    _ 最近瞥了一眼项目的重启脚本,发现运维一直在使用 kill-9<pid> 的方式重启 springboot embedded tomcat,其实大家几乎一致认为:kill-9<pi ...

  7. 【leetcode刷题笔记】Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. hd acm1008

    Problem Description The highest building in our city has only one elevator. A request list is made u ...

  9. android6.0 外部存储设备插拔广播以及获取路径(U盘)【转】

    本文转载自:https://blog.csdn.net/zhouchengxi/article/details/53982222 这里我将U盘作为例子来说明解析. android4.1版本时U盘插拔时 ...

  10. 常量池、perm(持久代)、方法区、栈

      常量池.perm(持久代).方法区.栈 常量池指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据. 除了包含代码中所定义的各种基本类型(如:int.long等等)和对象型(如St ...