给定一个长度为 n 的字符串,请编写一个函数判断该字符串是否回文。如果是回文请返回true,否则返回false。
字符串回文指该字符串正序与其逆序逐字符一致。
数据范围:
要求:空间复杂度
,时间复杂度
"absba"
true
"ranko"
false
"yamatomaya"
false
"a"
true
字符串长度不大于1000000,且仅由小写字母组成
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
public boolean judge (String str) {
int start = 0;
int end = str.length() - 1;
while (start <= end) {
if (str.charAt(start) != str.charAt(end)) return false;
start++;
end--;
}
return true;
}
}
public boolean judge (String str) {
// write code here
for(int i=0;i<str.length()/2;i++){
if(str.charAt(i)!=str.charAt(str.length()-1-i)){
return false;
}
}
return str!=null;
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
public boolean judge (String str) {
// write code here
//char[] chars = str.toCharArray();
if(str.length()<=1){
return true;
}
for(int i=0;i<str.length();i++){
if(str.charAt(i)!=str.charAt(str.length()-i-1)){
return false;
}
}
return true;
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
public boolean judge (String str) {
// write code here
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i))
return false;
}
return true;
}
} public boolean judge (String str) {
for(int start = 0,end = str.length() - 1 ; start < end; start++,end--){
if(str.charAt(start) != str.charAt(end)){
return false;
}
}
return true;
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
public boolean judge (String str) {
// write code here
int left = 0;
int right = str.length() - 1;
while(left <= right) {
if(str.charAt(left) != str.charAt(right))
return false;
left++;
right--;
}
return true;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
public boolean judge (String str) {
// write code here
if(str==null||str.length()<2) return true;
int left=0;
int right=str.length()-1;
while(left<=right){
if(str.charAt(left)!=str.charAt(right)) return false;
left++;
right--;
}
return true;
}
} public class Solution {
public boolean judge (String str) {
// write code here
StringBuilder builder = new StringBuilder(str);
builder.reverse();
if (str.equals(builder.toString()) ) {
return true;
}
return false;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
public boolean judge (String str) {
// write code here
// 转换成数组
char[] data = str.toCharArray();
int start = 0;
int end = str.length() - 1;
//前后,同时判断
while (start < end) {
// 前后的字符,一旦不一致就返回false
if (data[start] != data[end]) {
return false;
}
// start下标和end下标,往中间去
start++;
end--;
}
return true;
}
}