【leetcode刷题笔记】Valid Palindrome
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的更多相关文章
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode刷题笔记】Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode刷题笔记】Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode 刷题笔记 2. 有效的括号(Valid Parentheses)
tag: 栈(stack) 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须 ...
随机推荐
- vue-router实现页面的整体跳转
直接看效果图: 代码地址:https://github.com/YalongYan/vue-router-jump
- linux 下配置jdk
去java官方地址下载相应的源码包我下载的是1.8.0放在usr/local目录下 export JAVA_HOME=/usr/local/jdk1.8.0export PATH=$JAVA_HOME ...
- Git客户端搭建环境(Windows系统)的使用
本文环境: 操作系统:Windows XP SP3 Git客户端:TortoiseGit-1.8.8.0-32bit 一.安装Git客户端 全部安装均采用默认! 1. 安装支撑软件 msysgit: ...
- [Catalan数]1086 栈、3112 二叉树计数、3134 Circle
1086 栈 2003年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 栈是计算机中 ...
- 多语言中的“默认语言”设置
最近在搞一个多语言的东西,打算如果用户是中文环境就显示中文,其他任何非中文环境就显示英文.换句话说,把默认语言设置成英文. 不过因为VS是中文的,发现即使默认资源文件是英文(AppResource.r ...
- android菜鸟学习笔记21----ContentProvider(一)ContentProvider的简单使用
ContentProvider是Android四大组件之一,它用来封装数据,并通过ContentResolver接口将数据提供给其他应用.只有当需要在多个应用之间共享数据时才会用到ContentPro ...
- Android笔记之自定义的RadioGroup、RadioButton,以及View实例状态的保存与恢复
效果图 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- 转。git 乌龟的使用安装
TortoiseGit 简称 tgit, 中文名海龟Git. 海龟Git只支持神器 Windows 系统, 有一个前辈海龟SVN, TortoiseSVN和TortoiseGit都是非常优秀的开源的版 ...
- hive查询注意及优化tips
Hive是将符合SQL语法的字符串解析生成可以在Hadoop上执行的MapReduce的工具.使用Hive尽量按照分布式计算的一些特点来设计sql,和传统关系型数据库有区别, 所以需要去掉原有关系型数 ...
- 1django 视图与网址
创建一个项目,名字叫mysite django-admin startproject mysite(项目名) 成功后,看到如下样式 mysite ├── manage.py └── mysite ├─ ...