@@ -277,6 +277,42 @@ public class Solution {
277277}
278278```
279279
280+ #### C
281+
282+ ``` c
283+ char * longestPalindrome (char* s) {
284+ int n = strlen(s);
285+ bool** f = (bool** ) malloc(n * sizeof(bool* ));
286+ for (int i = 0; i < n; ++i) {
287+ f[ i] = (bool* ) malloc(n * sizeof(bool));
288+ for (int j = 0; j < n; ++j) {
289+ f[ i] [ j ] = true;
290+ }
291+ }
292+ int k = 0, mx = 1;
293+ for (int i = n - 2; ~ i; --i) {
294+ for (int j = i + 1; j < n; ++j) {
295+ f[ i] [ j ] = false;
296+ if (s[ i] == s[ j] ) {
297+ f[ i] [ j ] = f[ i + 1] [ j - 1 ] ;
298+ if (f[ i] [ j ] && mx < j - i + 1) {
299+ mx = j - i + 1;
300+ k = i;
301+ }
302+ }
303+ }
304+ }
305+ char* res = (char* ) malloc((mx + 1) * sizeof(char));
306+ strncpy(res, s + k, mx);
307+ res[ mx] = '\0';
308+ for (int i = 0; i < n; ++i) {
309+ free(f[ i] );
310+ }
311+ free(f);
312+ return res;
313+ }
314+ ```
315+
280316#### Nim
281317
282318```nim
@@ -527,55 +563,6 @@ class Solution {
527563}
528564```
529565
530- #### c
531-
532- ``` c
533- char *longestPalindrome (char * s) {
534- int n = strlen(s);
535- if (n == 0) {
536- char * result = malloc(1);
537- result[ 0] = '\0';
538- return result;
539- }
540-
541- bool dp[ n] [ n ] ;
542- memset(dp, 0, sizeof(dp));
543-
544- int start = 0, max_len = 1;
545-
546- for (int i = 0; i < n; i++) {
547- dp[ i] [ i ] = true;
548- }
549-
550- for (int i = 0; i < n - 1; i++) {
551- if (s[ i] == s[ i + 1] ) {
552- dp[ i] [ i + 1 ] = true;
553- start = i;
554- max_len = 2;
555- }
556- }
557-
558- // Check for lengths > 2
559- for (int len = 3; len <= n; len++) {
560- for (int i = 0; i < n - len + 1; i++) {
561- int j = i + len - 1;
562- if (s[ i] == s[ j] && dp[ i + 1] [ j - 1 ] ) {
563- dp[ i] [ j ] = true;
564- if (len > max_len) {
565- start = i;
566- max_len = len;
567- }
568- }
569- }
570- }
571- char * result = malloc(max_len + 1);
572- strncpy(result, s + start, max_len);
573- result[ max_len] = '\0';
574- return result;
575- }
576-
577- ```
578-
579566<!-- tabs: end -->
580567
581568<!-- solution: end -->
0 commit comments