首页 > 代码库 > leecode 回文字符串加强版
leecode 回文字符串加强版
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.
只考虑数字和字母,字母大小写都一样,判断是否是回文。重点是忽略非非字母和数字。比较简单。
1 public class Solution { 2 public boolean isPalindrome(String s) { 3 4 int low=0; 5 int high=s.length()-1; 6 String s1=s.toLowerCase(); 7 while(low<high) 8 { 9 10 while(low<high)11 {12 char c=s1.charAt(low);13 if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘))14 {15 break;16 }17 else low++;18 19 }20 21 while(low<high)22 {23 char c=s1.charAt(high);24 if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘))25 {26 break;27 }else high--;28 29 }30 31 32 33 if(s1.charAt(low)==s1.charAt(high)) 34 {35 low++;36 high--;37 }38 else39 {40 return false;41 }42 43 44 45 }46 47 return true;48 49 50 }51 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。