首页 > 试题广场 >

判断是否为回文字符串

[编程题]判断是否为回文字符串
  • 热度指数: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,且仅由小写字母组成
bool projudge(char* str , int len) {
    if(len<2)
        return true;
    if(projudge(str+1, len-2) && str[0]==str[len-1]) 
        return true;
    else 
        return false;
}
bool judge(char* str ) {
    return projudge(str, strlen(str));
}

编辑于 2024-03-24 13:27:07 回复(1)
bool judge(char* str ) {
    // write code here
    if (*(str+1) == '\0') {
        return true;
    }
    char* p1 = str;
    char* p2 = str;
    int count = 0;
    while (*p1 != '\0') {
        p1++;
        count++;
    }
    p1--;
    count = count / 2;
    while (count--) {
        if (*p1 != *p2) {
            return false;
        }
        p1--;
        p2++;
    }
    return true;
}
发表于 2023-09-10 20:59:18 回复(0)
#include <string.h>
bool judge(char* str ) {
    int  iLength = strlen(str);
    if(iLength == 0 || iLength == 1 )
    {
        return true;
    }
    char *pt1 = &str[0];
    char *pt2 = &str[iLength-1];
    while(pt1 < pt2)
    {
        if(*pt1 == *pt2)
        {
            pt1++;
            pt2--;
        }
        else if (*pt1 != *pt2)
        {
            return false;
        }

        if(pt1 == pt2)
        {
            break;
        }
       
    }
    return true;
}
发表于 2023-02-18 16:16:09 回复(0)

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * @param str string字符串 待判断的字符串
 * @return bool布尔型
 */
#include <stdbool.h>
#include <string.h>
/*使用双指针方式*/
bool judge(char* str ) {
    // write code here
    int str_len=0;
    char *head_point=NULL;
    char *tail_point=NULL;
    /*如果为空就退出*/
    if(str==NULL)
        return false;

    /*计算出字符串的长度*/
    str_len=strlen((const char*)str);
    /*如果只有一个字符也直接返回真*/
    if(str_len==1)
        return true;

    head_point=str;
    tail_point=str+str_len-1;
    for(;head_point<tail_point;head_point++,tail_point--)
    {
        if(*head_point!=*tail_point)
        {
            return false;
        }
    }
   
    return true;
}
发表于 2023-02-03 11:49:47 回复(0)
bool judge(char* str ) {
    // write code here
    for (int i = 0; i < strlen(str) / 2; i++) {
        if (str[i] != str[strlen(str) - i - 1]) {
            return false;
        }
    }
    return true;
}

bool judge(char* str ) {
    // write code here
    int i = 0, j = strlen(str) - 1;
    while (i < j) {
        if (str[i] == str[j]) i++, j--;
        else return false;
    }
    return true;
}
为啥上面那个超时?????感觉他的复杂度还低点呢……
发表于 2022-06-28 23:39:11 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 
 * @param str string字符串 待判断的字符串
 * @return bool布尔型
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
#include<stdbool.h>
bool judge(char* str ) {
    // write code here
    int len = strlen(str);
    int i = 0;
    while(i<len){
        if(str[i] != str[len-1]){
           return false; 
        }
        i++;
        len--;
    }
    
    return true;
}
发表于 2022-05-02 12:35:35 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 
 * @param str string字符串 待判断的字符串
 * @return bool布尔型
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
bool judge(char* str )
{
    if(strlen(str) > 1000000)
        return false;
    int J_O = strlen(str) % 2;
    if(J_O)
    {
        int start_location = strlen(str) / 2;
        for(int i = 1; i <= strlen(str) / 2; i++)
        {
            if(str[start_location + i] == str[start_location - i])
                continue;
            else
                return false;
        }
    }
    else
    {
        int start_location1 = strlen(str) / 2;
        int start_location2 = strlen(str) / 2 - 1;
        for(int i = 0; i < strlen(str) / 2; i++)
        {
            if(str[start_location1 + i] == str[start_location2 - i])
                continue;
            else 
                return false;
        }
    }
    return true;
}
发表于 2022-02-17 17:45:50 回复(0)
发表于 2022-01-07 18:39:44 回复(1)
bool judge(char* str ) 
{
    int len=strlen(str);
    char*end=str+len-1;
    while(str<end)
    {
        if(*str++!=*end--)
        {
            return false;
        }
    }
    return true;
}
发表于 2021-12-21 01:26:06 回复(0)
本题可采用两遍同时向中间遍历的方法,判断数组两端的变量是否相等,不相等直接返回false,当遍历到left>=right时,循环结束,则返回true
bool judge(char* str ) {
    // write code here
    int left,right;
    int i = 0;
    int n = strlen(str);
    left = 0;
    right = n-1;
    while(left<right)
    {
        if(str[left]!=str[right])
        {
            return false;
        }
        left++;
        right--;
           
    }
    return true;
    
    
}

发表于 2021-11-30 18:28:40 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 
 * @param str string字符串 待判断的字符串
 * @return bool布尔型
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
bool judge(char* str ) {
    // write code here
    int i,c=0;
    int length=strlen(str);
    for(i=0;i<length/2;i++){
        if(str[i]!=str[length-i-1]){
           return false;
        }   
    }
    return true;

}

发表于 2021-11-28 11:42:20 回复(0)
bool judge(char* str ) {
    // write code here
    int i,j,len;
    char *p;
    p=str;
    len=strlen(str);
    for(i=0,j=len-1;i<len;i++,j--)
        if(p[i]!=p[j])
          return 0;
    return 1;
}
发表于 2021-09-04 10:57:05 回复(0)
bool judge(char* str ) {
    // write code here
    int len = strlen(str);
    bool result = true;
    for(int i = 0; i < strlen(str)/2; i++){
        if(str[i] != str[len-i-1]){
            result = false;
        }
    }
    return result;
}

代码执行速度很快,不清楚为啥占内存很大
发表于 2021-07-25 00:01:32 回复(0)
#include <stdbool.h>   /* */

bool judge(char* str )
 {
    // write code here
    int strLen = 0;
    int strHalfLen=0;
    /* 字长 */
    strLen = strlen(str);
    strHalfLen = strLen/2;
    /* 回文每个位置比较*/
    for (int i=0;i<strHalfLen;i++)
    {
        if (str[i] != str[strLen-i-1])
        {
            return false;
        }
    }
    /* 一个字符串直接到这,不用考虑异常 */
    return true;
}

重点有两处:
1.bool头文件,用int替换输出类型好像也可以
2.strlen求长,不能用sizeof(求内存长度)

编辑于 2021-07-18 19:44:54 回复(0)

问题信息

难度:
16条回答 11348浏览

热门推荐

通过挑战的用户

查看代码