Valid Palindrome
来源:互联网 发布:sql语句排序查询 编辑:程序博客网 时间:2024/05/05 00:37
https://oj.leetcode.com/problems/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.
public boolean isPalindrome(String s)
本题算法如下:
这题其实很简单,就是头尾指针往中间扫,跳过所有非法字符(不是a~z之间的字符均非法),匹配头尾指针的内容即可。
public boolean isPalindrome(String s) { int head = 0, tail = s.length() - 1; s = s.toLowerCase(); while(head < tail){ while((s.charAt(head) > 'z' || s.charAt(head) < 'a') && (s.charAt(head) > '9' || s.charAt(head) < '0') && head < tail) head++; while(s.charAt(tail) > 'z' || s.charAt(tail) < 'a' && (s.charAt(tail) > '9' || s.charAt(tail) < '0')&& head < tail) tail--; if(s.charAt(head) != s.charAt(tail)) return false; head++; tail--; } return true; }
0 0
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- valid palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- PHP实现伪造邮件攻击
- Struts 2配置
- 【Jason's_ACM_解题报告】Help is needed for Dexter
- LeetCode-Remove Duplicates from Sorted Array
- Codeforces 512B Fox And Jumping dp+gcd
- Valid Palindrome
- 浅析HTTP协议
- 接口协议
- Java web开发中的编码问题
- hdu 1558 Segment set【基础带权并查集+计算几何】
- 关于js匿名函数和闭包
- MongoDB upsert操作并发的原子性
- 关于一条不会被自动踢掉的系统后台命令
- 想成为嵌入式程序员应知道的0x10个基本问题