首页 > 试题广场 >

判断是否为回文字符串

[编程题]判断是否为回文字符串
  • 热度指数:93152 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个长度为 n 的字符串,请编写一个函数判断该字符串是否回文。如果是回文请返回true,否则返回false。

字符串回文指该字符串正序与其逆序逐字符一致。

数据范围:
要求:空间复杂度 ,时间复杂度
示例1

输入

"absba"

输出

true
示例2

输入

"ranko"

输出

false
示例3

输入

"yamatomaya"

输出

false
示例4

输入

"a"

输出

true

备注:
字符串长度不大于1000000,且仅由小写字母组成
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;
    }
}

发表于 2024-03-27 14:41:45 回复(0)
import java.util.*;

public class Solution {
    public boolean judge (String str) {
        // write code here
                String s = new StringBuilder(str).reverse().toString();
            if(s.equals(str)){
                return true;
            }else {
                return false;
            }
    }
}
发表于 2024-01-28 20:57:04 回复(0)
不是,java排行榜快的那几个全是一次判断就return了,代码都是错的也能占榜首?3年前的用例有问题啊
编辑于 2024-01-26 21:19:59 回复(0)
public boolean judge (String str) {
    // write code here
    int p1=0 ,p2=str.length()-1;
    while(p1<p2){
        if(str.charAt(p1++)!=str.charAt(p2--)){
            return false;
        }
    }
    return true;
}

发表于 2023-12-13 22:28:53 回复(0)
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;
    }
}

发表于 2023-11-03 15:14:18 回复(0)
   
 public boolean judge (String str) {
        // write code here
        StringBuffer sb = new StringBuffer(str);
        String s = sb.reverse().toString();
       return s.equals(str);
       }


发表于 2023-09-11 17:33:37 回复(0)
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;
    }

发表于 2023-08-02 20:24:30 回复(0)
import java.util.*;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * @param str string字符串 待判断的字符串
     * @return bool布尔型
     */
    public boolean judge (String str) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i<str.length();i++){
            char ch = str.charAt(i);
            stack.push(ch);
        }
        for(int i = 0;i<str.length();i++){
            char ch = str.charAt(i);
            char ch1=stack.pop();
            if(ch!=ch1){
                return false;
            }
        }
        return true;
    }
}
发表于 2023-05-26 00:34:52 回复(1)
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;
    }
}

发表于 2023-04-28 17:04:58 回复(0)
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;
    }
}

发表于 2023-04-12 20:05:34 回复(0)
//双指针
public boolean judge (String str) {
        char[] c = str.toCharArray();
        // write code here
        int left = 0, right = str.length() - 1;
        while (left <= right) {
           if(c[left] != c[right]){
            return false;
           }
           left += 1;
           right -= 1;
        }
        return true;
    }
发表于 2023-04-10 20:05:54 回复(0)
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;
    }
}

发表于 2023-03-20 11:20:29 回复(0)
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;
    }
}

发表于 2023-03-07 16:01:10 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param str string字符串 待判断的字符串
     * @return bool布尔型
     */
    public boolean judge (String str) {
        // write code here
        int i=0,j=str.length()-1;
        while(i<=j){
            if(str.charAt(i)==str.charAt(j)){
                i++;
                j--;
            }else{
                break;
            }
        }
        if(i<j){return false;}
        else{return true;}
    }
}

发表于 2022-08-16 13:15:15 回复(0)
import java.util.*;
public class Solution {
    public boolean judge (String str) {
        int left = 0, right = str.length() - 1;
        while(left < right){
            if(str.charAt(left++) != str.charAt(right--)) return false;
        }
        return true;
    }
}

发表于 2022-08-07 15:02:16 回复(0)
入门级的题不应该不会做
import java.util.*;
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param str string字符串 待判断的字符串
     * @return bool布尔型
     */
    public boolean judge (String str) {
        // write code here
        if(str.length() == 0 || str.length() == 1){
            return true;
        }
        int l = 0;
        int r = str.length() - 1;
        while(l < r){
            if(str.charAt(l) != str.charAt(r)){
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
}

发表于 2022-05-03 15:47:26 回复(0)
public class Solution {
    public boolean judge (String str) {
        // write code here
        return new StringBuilder(str).reverse().toString().equals(str);
    }
}

发表于 2022-04-16 10:48:21 回复(0)

问题信息

难度:
60条回答 11346浏览

热门推荐

通过挑战的用户

查看代码