判断题目给出的字符串是不是回文,仅考虑字符串中的字母字符和数字字符,并且忽略大小写
例如:"nowcoder Is Best tsebsi: redoc won"是回文
"race a car"不是回文
注意:
你有没有考虑过字符串可能为空?这是面试时应该提出的一个好问题。
针对这个问题,我们定义空字符串是回文
"nowcoder Is Best tsebsi: redoc won"
true
"race a car"
false
import java.util.*;
public class Solution {
/**
*
* @param s string字符串
* @return bool布尔型
*/
public boolean isPalindrome (String s) {
// write code here
s=s.toLowerCase();
int i=0,j=s.length()-1;
while(i<j)
{
while(i<j&&(s.charAt(i)<'a'||s.charAt(i)>'z')&&(s.charAt(i)<'0'||s.charAt(i)>'9'))
i++;
while(i<j&&(s.charAt(j)<'a'||s.charAt(j)>'z')&&(s.charAt(j)<'0'||s.charAt(j)>'9'))
j--;
if(s.charAt(i)!=s.charAt(j))
return false;
else
{
i++; j--;
}
}
return true;
}
} public boolean isPalindrome (String s) {
if(s == null) return false;
if(s != null && s.length() == 0) return true;
//双指针-左右指针
int left = 0;
int right = s.length() - 1;
char[] array = s.toCharArray();
while(left < right){
//处理非英文字母和空格
while(left < right && !((array[left] >= 'a' && array[left] <= 'z') || (array[left] >= 'A' && array[left] <= 'Z') || (array[left] >= '0' && array[left] <='9'))){
left++;
}
while(left < right && !((array[right] >= 'a' && array[right] <= 'z') || (array[right] >= 'A' && array[right] <= 'Z') || (array[right] >= '0' && array[right] <='9'))){
right--;
}
//左右指针对应数组元素匹配比较
if(!(
(((array[left] >= 'a' && array[left] <= 'z') || (array[left] >= 'A' && array[left] <= 'Z')) &&
((array[right] >= 'a' && array[right] <= 'z') || (array[right] >= 'A' && array[right] <= 'Z')) &&
//通过ASCIL区分大小写
((array[left] + 32 == array[right]) || (array[left] - 32 == array[right])))
|| array[left] == array[right])){
return false;
}
left++;
right--;
}
//匹配成功
return true;
} import java.util.*;
public class Solution {
/**
* @param s string字符串
* @return bool布尔型
*/
public boolean isPalindrome (String s) {
s=s.replace(" ","");//去掉空格
char[]str_list=s.toCharArray();
//遍历,忽略非法字符
int i=0,j=str_list.length-1,n1,n2;
while (i<j){//逐个对比,遇到非法字符忽略
while(i<str_list.length&&!Islegal(str_list[i]))
i++;
if(i==str_list.length) return true;//如果走到头说明都是非法字符,返回true
while(j>=i&&!Islegal(str_list[j]))
j--;
n1=str_list[i];
n2=str_list[j];
if(n1==n2||Math.abs(n1-n2)==32){//要么相等,要么是大小写的关系
i++;j--;
}
else
break;
}
if(i>=j)
return true;
else
return false;
}
static boolean Islegal(char cha){//判断一个字符是否为合法字符(即数字或字母)
if((cha>='a'&&cha<='z')||(cha>='A'&&cha<='Z')||(cha>='0'&&cha<='9'))
return true;
return false;
}
} public boolean isPalindrome (String s) {
/*
非递归
时间复杂度:O(n)
空间复杂度:
*/
//为空的情况
if(s==null){
return false;
}
//忽略大小写,统一转成大写或者小写
String strLower = s.toLowerCase();
//转成字符数组
char[] str = strLower.toCharArray();
ArrayList list = new ArrayList();
//遍历字符数组,筛选符合条件的元素存储到list中
for(int i=0;i<str.length;i++){
if((str[i]>='0'&&str[i]<='9')||(str[i]>='a'&&str[i]<='z')){
list.add(str[i]);
}
}
//一个从头,一个从尾,两两比较
int j = list.size()-1;
for(int i=0;i<list.size();i++){
if(list.get(i)==list.get(j)||i>j){
j--;
}else{
return false;
}
}
return true;
} public class Solution {
public boolean isPalindrome(String s) {
//添加一个判断字符是否为字符或数字的方法,如果不是则字符串下面的
//位置加1,比较之前还需要把字符通过Character.toLowerCase(s.charAt(i))
//转换成小写的字符
int begin = 0;
int end = s.length() - 1;
while(begin < end)
{
while(!isCharOrNum(s.charAt(begin)) && begin < end)
{begin ++;}
while(!isCharOrNum(s.charAt(end)) && begin < end)
{end --;}
if(Character.toLowerCase(s.charAt(begin)) !=
Character.toLowerCase(s.charAt(end)))
return false;
begin ++;
end --;
}
return true;
}
public boolean isCharOrNum(char c)
{
if((c >= '0' && c <= '9')
|| (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'))
return true;
return false;
}
} 运行时间:165ms
占用内存:16564k
*/不通过 请问为什么会报错????????cry,搞了一小时了到底哪个数据有问题
import java.util.*;
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.equals("")) {
return true;
}
s = s.toLowerCase();
s = s.replaceAll("[^a-zA-Z0-9]", "");
char[] chs = s.toCharArray();
Stack<Character> stack = new Stack<>();
for (int i=0; i < chs.length; i++) {
stack.push(chs[i]);
}
for (int i=0; i < chs.length; i++) {
if (chs[i] != stack.pop()) {
break;
}
}
if (stack.isEmpty()) {
return true;
} else {
return false;
}
}
} public boolean isPalindrome(String s) {
if (s == null || s.equals("")) {
return true;
}
s = s.toLowerCase(); // 忽略大小写
s = s.replaceAll("[^a-zA-Z0-9]", ""); // 只保留字母和数字
if (s.equals("")) {
return true;
}
char[] chs = s.toCharArray();
return isPalindrome(chs, 0);
}
public boolean isPalindrome(char[] chs, int cur) {
if (cur == chs.length) {
return true;
}
return (chs[cur] == chs[chs.length-1-cur]) && isPalindrome(chs, cur+1);
}
//大佬们帮我看一下 我这程序有什么问题!是编辑器出问题了还是我的代码错了
//我真的好无语啊,之前的题目也有这样的现象
public boolean isPalindrome(String s) {
StringBuilder ss = new StringBuilder();
for (char c : s.toCharArray()) ss.append(Character.isLetter(c) ? Character.toLowerCase(c) : "");
return isPalin(ss);
}
public static boolean isPalin(StringBuilder ss) {
int i = 0;
int j = ss.length() - 1;
while (i < j && ss.charAt(i++) == ss.charAt(j--)) ;
return i >= j;
} public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0)
return true;
StringBuilder buffer = new StringBuilder();
String temp = s.toLowerCase();
char tempChar = ' ';
for (int i = 0; i < temp.length(); ++i) {
tempChar = temp.charAt(i);
if ((tempChar <= 'z' && tempChar >= 'a') || (tempChar <= '9' && tempChar >= '0'))
buffer.append(tempChar);
}
if (buffer.length() == 0)
return true;
int start = 0;
int end = buffer.length() - 1;
boolean isPalindrome = true;
while (start < end) {
if (buffer.charAt(start) != buffer.charAt(end))
return false;
++start;
--end;
}
return isPalindrome;
}
} import java.util.*;
public class Solution {
public boolean isPalindrome(String s) {
if(s==""||s==null)
return true;
int l=0,r=s.length()-1;
while(l<r){
while(l<s.length()&&!judge(s.charAt(l)))
l++;
while(r>=0&&!judge(s.charAt(r)))
r--;
if(l>=s.length()||r<0)
return true;
char a=s.charAt(l);
char b=s.charAt(r);
if(a==b||Math.abs(a-b)==32){
l++;
r--;
}
else
return false;
}
return true;
}
public boolean judge(char c){
if((c<='9'&&c>='0')||(c<='z'&&c>='a')||(c<='Z'&&c>='A'))
return true;
else
return false;
}
}
public class Solution {
public static boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
while (i < j) {
while (i < j && ! (Character.isDigit(s.charAt(i)) || Character.isLetter(s.charAt(i))))
i ++;
while (i < j && ! (Character.isDigit(s.charAt(j)) || Character.isLetter(s.charAt(j))))
j --;
if(i < j && Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) return false;
i ++;
j --;
}
return true;
}
}
public class Solution { public boolean isPalindrome(String s) { if (s.isEmpty()) { return true; } int head = 0, tail = s.length() - 1; char cHead, cTail; while(head <= tail) { cHead = s.charAt(head); cTail = s.charAt(tail); if (!Character.isLetterOrDigit(cHead)) { head++; } else if(!Character.isLetterOrDigit(cTail)) { tail--; } else { if (Character.toLowerCase(cHead) != Character.toLowerCase(cTail)) { return false; } head++; tail--; } } return true; } }