5.最长的回文子串
暴力解法 思路 1、做一个子函数,用于检测输入的字符串是否是回文串 2、使用双指针,头指针从字符串开始处遍历,尾指针每次均从结尾处开始,检查头尾指针之间的字符串是否是回文串,若是,且长度大于之前的长度,则更新,否则进行下次检查,注意,大循环的结束条件可以随着找到回文子串的长度而更新。 代码 # include <stdio.h> # include <string.h> # include <stdlib.h> // for malloc(), free() # define YES 1 # define NO 0 int IsPalindrome ( char * strIn , int strLength ) { for ( int i = 0 ; i < strLength / 2 ; i ++ ) { if ( strIn [ i ] != strIn [ strLength - 1 - i ] ) { return NO ; } } return YES ; } char * longestPalindrome ( char * s ) { int strLength = strlen ( s ) ; if ( strLength < 2 ) { return s ; } char tmpLongestPald [ 1001 ] = { 0 } ;