If a string is Palindromic
来源:互联网 发布:java 字符串分割 编辑:程序博客网 时间:2024/04/28 14:46
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.
这个题相当直观,这次做的时候我直接call Java String里面的toLowerCase, 这样就不需要考录比如 ‘X’ 和 ‘x' 其实是同一个字母的问题了
public class Solution { public boolean isPalindrome(String s) { s = s.trim().toLowerCase(); int len = s.length(); int start = 0, end = len-1; while(start < end){ // if it terminates with start == end, it is a palindrome: e.g. "aba" while(start < end && !isAlphaNumeric(s.charAt(start))) start ++; while(start < end && !isAlphaNumeric(s.charAt(end))) end--; if(start == end) break; if(s.charAt(start) != s.charAt(end)) return false; else{ start++; end --; } } return true; } public boolean isAlphaNumeric(char ch){ if(ch >= 'a' && ch <= 'z') return true; else if(ch <= '9' && ch >= '0') return true; else return false; }}
0 0
- If a string is Palindromic
- test if a string is a number
- Check if a string value is numeric
- test if a string is an integer?
- 【6】Decide if one string is a rotation of another string or not
- If God is a Programmer
- A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi
- DP33 两个字符串交叉得到的字符串 Find if a string is interleaved of two other strings @geeksforgeeks
- detect if a URL is reachable
- Determining if a View is Hardware Accelerated
- Check if a linked list is palindrome
- string rotation (if string is the rotate of the other)
- Check whether a string is a palindrome
- decide weather a string is a palindrome
- Q2.7 Check if a list is a palindrome
- String b = a.intern(); if (b == a)
- EasyTest would get stuck if the json string is corrupted
- What is a valid JSON string?
- 【Leetcode】Search a 2D Matrix
- SQL中索引的原理
- 判断互质数的方法
- 之前自己建的blog
- org/objectweb/asm/Type异常解决办法
- If a string is Palindromic
- Java获取本机IP列表的几种方法
- 常用的Javascript设计模式
- 老杜(杜昶旭)GRE填空笔记部分整理-by“ 1哥”+ TTC相关资料
- Find Minimum in Rotated Sorted Array II
- 【Leetcode】Pow(x,n)
- Minimum Window Substring
- 【水打表】#69 A. Panoramix's Prediction
- POJ 3715:计算工作天数