【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) 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须 ...
随机推荐
- windows上mysql安装
1. 下载MySQL Community Server 5.7.14 Index of /MySQL/Downloads/MySQL-Cluster-7.1 2. 解压MySQL压缩包 安装路径:E: ...
- iOS CLLocationManager定位
本文转载至 http://www.tuicool.com/articles/7JBRZn 在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法 ...
- SQL SERVER 2008递归
tab1 表结构: create tab1 ( id int primary key identity(1,1), parentid int not null, name varchar(25) ) ...
- Java 学习 day09
01-面向对象(内部类访问规则) package myFirstCode; /* 内部类的访问规则: 1. 内部类可以直接访问外部类的成员,包括私有private. 之所以可以直接访问外部类中的成员, ...
- 爬虫入门【7】Python-文件的读写和JSON
文本文档的读写 最重要的open()方法将返回一个file对象,经常使用的两个参数为open(filename,mode) 其中,filename为file保存的地址,可以是本地地址,相对地址或者绝对 ...
- SELinux状态修改
查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- sqlserver删除所有表
--/第1步**********删除所有表的外键约束*************************/ DECLARE c1 cursor for select 'alter table ['+ o ...
- Feign-独立使用-实战
目录 写在前面 1.1.1. 短连接API的接口准备 1.1.2. 申明远程接口的本地代理 1.1.3. 远程API的本地调用 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Jav ...
- Jquery禁用所有checkbox
$("input[type=checkbox]").each(function(){ $(this).attr("disabled",false);});
- CentOS7安装MySQL8.0小计
之前讲配置文件和权限的时候有很多MySQL8的知识,有同志说安装不太一样,希望发个文,我这边简单演示一下 1.环境安装 下载MySQL提供的CentOS7的yum源 官方文档:<https:// ...