Leetcode 详解(valid plindrome)
Question: 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.
Example Questions Candidate Might Ask:
Q: What about an empty string? Is it a valid palindrome?
A: For the purpose of this problem, we define empty string as valid palindrome.
Solution:
O(n) runtime, O(1) space:
The idea is simple, have two pointers – one at the head while the other one at the tail.
Move them towards each other until they meet while skipping non-alphanumeric
characters.
Consider the case where given string contains only non-alphanumeric characters. This is
a valid palindrome because the empty string is also a valid palindrome.
public boolean isPalindrome(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++; //Character.asLetterOrDigit() 用来判断是不是字母或者数字
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) j--;
if (Character.toLowerCase(s.charAt(i))!= Character.toLowerCase(s.charAt(j))) {
return false;
}
i++; j--;
}
return true;
}
注:这道题类似于用了两个指针在数组两头,同时,while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++ 用的很巧妙,避免了很多判断。
Leetcode 详解(valid plindrome)的更多相关文章
- Leetcode 详解(Valid Number)
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- Leetcode 详解(ReverseWords)
Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...
- Leetcode 详解(股票交易日)(动态规划DP)
问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
- Leetcode详解Maximum Sum Subarray
Question: Find the contiguous subarray within an array (containing at least one number) that has the ...
- Leetcode 详解(Substing without repeats character)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- The Skyline Problem leetcode 详解
class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...
- 访问Storm ui界面,出现org.apache.storm.utils.NimbusLeaderNotFoundException: Could not find leader nimbus from seed hosts ["master"]. Did you specify a valid list of nimbus hosts for confi的问题解决(图文详解)
不多说,直接上干货! 前期博客 apache-storm-0.9.6.tar.gz的集群搭建(3节点)(图文详解) apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)( ...
随机推荐
- Java Programming Language Enhancements
引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...
- Latex环境安装
安装latex环境 sudo apt-get install texlive-full 推荐使用texstudio编辑器,简单方便高效靠谱 sudo apt-get install texstudio ...
- PHP网页
1.安装YUM源 2.安装httpd与PHP yum install httpd -y yum install php -y 3.进入htmi文件中 cd /var/www/html/ 4.将自己编写 ...
- reactive-turtle
reactive-turtle 标签(空格分隔): Scala Github 偶然在Github上看到一个有意思的小项目,一个粑粑教11岁的儿子学习scala,因为学习语法等太枯燥了,所以写了一个海龟 ...
- 关于phpcms 万一忘记密码怎么破?
莫慌~海洋小生教你~我也是偷偷学来的,呀哈哈哈! first:............................你就认命吧!哈哈哈... 开玩笑开玩笑! LOOK HERE ↓: 1.在没有安装 ...
- Hibernate的性能优化问题
本文是根据Hibernate帮助文档,以及一些书籍及项目经验整理而成,只提供要点和思路,具体做法可以留言探讨,或是找一些更详细更有针对性的资料. 初用Hibernate的人也许都遇到过性能问题,实现同 ...
- MySQL 添加列, 修改列, 删除列
ALTER TABLE:添加,修改,删除表的列,约束等表的定义. 查看列:desc 表名; 查看数据库创建语句: show create database 数据库名: 查看创建表的语句: show c ...
- CPU过高的排查方法
一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. (友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hank ...
- Tomcat安装配置
Tomcat安装配置 很久没有通过博客对学习所得进行记录了. 现在将使用Tomcat的一些经验和心得写到这里,作为记录和备忘.如果有朋友看到,也请不吝赐教. 1.首先是Tomcat的获取和安装. 获取 ...
- (原创)QuartusII设置虚拟引脚(Virtual Pin)
方法一: 在Quartus II中Assignments->Assignment Editor, 在Category栏选择logic options, 到列表中To列下添加要设置的引脚接口,如果 ...