File tree Expand file tree Collapse file tree 6 files changed +241
-0
lines changed
0005.Longest Palindromic Substring Expand file tree Collapse file tree 6 files changed +241
-0
lines changed Original file line number Diff line number Diff line change @@ -527,6 +527,56 @@ class Solution {
527527}
528528```
529529
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+
579+
530580<!-- tabs:end -->
531581
532582<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -525,6 +525,55 @@ class Solution {
525525}
526526```
527527
528+ #### C
529+
530+ ``` c
531+ char *longestPalindrome (char * s) {
532+ int n = strlen(s);
533+ if (n == 0) {
534+ char * result = malloc(1);
535+ result[ 0] = '\0';
536+ return result;
537+ }
538+
539+ bool dp[ n] [ n ] ;
540+ memset(dp, 0, sizeof(dp));
541+
542+ int start = 0, max_len = 1;
543+
544+ for (int i = 0; i < n; i++) {
545+ dp[ i] [ i ] = true;
546+ }
547+
548+ for (int i = 0; i < n - 1; i++) {
549+ if (s[ i] == s[ i + 1] ) {
550+ dp[ i] [ i + 1 ] = true;
551+ start = i;
552+ max_len = 2;
553+ }
554+ }
555+
556+ // Check for lengths > 2
557+ for (int len = 3; len <= n; len++) {
558+ for (int i = 0; i < n - len + 1; i++) {
559+ int j = i + len - 1;
560+ if (s[ i] == s[ j] && dp[ i + 1] [ j - 1 ] ) {
561+ dp[ i] [ j ] = true;
562+ if (len > max_len) {
563+ start = i;
564+ max_len = len;
565+ }
566+ }
567+ }
568+ }
569+ char * result = malloc(max_len + 1);
570+ strncpy(result, s + start, max_len);
571+ result[ max_len] = '\0';
572+ return result;
573+ }
574+
575+ ```
576+
528577<!-- tabs:end -->
529578
530579<!-- solution:end -->
Original file line number Diff line number Diff line change 1+
2+
3+ char * longestPalindrome (char * s ) {
4+ int n = strlen (s );
5+ if (n == 0 ) {
6+ char * result = malloc (1 );
7+ result [0 ] = '\0' ;
8+ return result ;
9+ }
10+
11+ bool dp [n ][n ];
12+ memset (dp , 0 , sizeof (dp ));
13+
14+ int start = 0 , max_len = 1 ;
15+
16+ for (int i = 0 ; i < n ; i ++ ) {
17+ dp [i ][i ] = true;
18+ }
19+
20+ for (int i = 0 ; i < n - 1 ; i ++ ) {
21+ if (s [i ] == s [i + 1 ]) {
22+ dp [i ][i + 1 ] = true;
23+ start = i ;
24+ max_len = 2 ;
25+ }
26+ }
27+
28+ // Check for lengths > 2
29+ for (int len = 3 ; len <= n ; len ++ ) {
30+ for (int i = 0 ; i < n - len + 1 ; i ++ ) {
31+ int j = i + len - 1 ;
32+ if (s [i ] == s [j ] && dp [i + 1 ][j - 1 ]) {
33+ dp [i ][j ] = true;
34+ if (len > max_len ) {
35+ start = i ;
36+ max_len = len ;
37+ }
38+ }
39+ }
40+ }
41+ char * result = malloc (max_len + 1 );
42+ strncpy (result , s + start , max_len );
43+ result [max_len ] = '\0' ;
44+ return result ;
45+ }
Original file line number Diff line number Diff line change @@ -493,6 +493,40 @@ class Solution {
493493}
494494```
495495
496+ #### C
497+
498+ ``` C
499+ char *convert (char * s, int numRows) {
500+ if (numRows == 1 || numRows >= strlen(s)) {
501+ char * result = malloc(strlen(s) + 1);
502+ strcpy(result, s);
503+ return result;
504+ }
505+ char ** rows = malloc(numRows * sizeof(char * ));
506+ for (int i = 0; i < numRows; i++) {
507+ rows[ i] = malloc(strlen(s) + 1);
508+ rows[ i] [ 0 ] = '\0';
509+ }
510+ int currentRow = 0;
511+ int goingDown = 0;
512+ for (int i = 0; s[ i] != '\0'; i++) {
513+ strncat(rows[ currentRow] , &s[ i] , 1);
514+ if (currentRow == 0 || currentRow == numRows - 1) {
515+ goingDown = !goingDown;
516+ }
517+ currentRow += goingDown ? 1 : -1;
518+ }
519+ char * result = malloc(strlen(s) + 1);
520+ result[ 0] = '\0';
521+ for (int i = 0; i < numRows; i++) {
522+ strcat(result, rows[ i] );
523+ free(rows[ i] );
524+ }
525+ free(rows);
526+ return result;
527+ }
528+ ```
529+
496530<!-- tabs:end -->
497531
498532<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -491,6 +491,40 @@ class Solution {
491491}
492492```
493493
494+ ####
495+
496+ ``` c
497+ char *convert (char * s, int numRows) {
498+ if (numRows == 1 || numRows >= strlen(s)) {
499+ char * result = malloc(strlen(s) + 1);
500+ strcpy(result, s);
501+ return result;
502+ }
503+ char ** rows = malloc(numRows * sizeof(char * ));
504+ for (int i = 0; i < numRows; i++) {
505+ rows[ i] = malloc(strlen(s) + 1);
506+ rows[ i] [ 0 ] = '\0';
507+ }
508+ int currentRow = 0;
509+ int goingDown = 0;
510+ for (int i = 0; s[ i] != '\0'; i++) {
511+ strncat(rows[ currentRow] , &s[ i] , 1);
512+ if (currentRow == 0 || currentRow == numRows - 1) {
513+ goingDown = !goingDown;
514+ }
515+ currentRow += goingDown ? 1 : -1;
516+ }
517+ char * result = malloc(strlen(s) + 1);
518+ result[ 0] = '\0';
519+ for (int i = 0; i < numRows; i++) {
520+ strcat(result, rows[ i] );
521+ free(rows[ i] );
522+ }
523+ free(rows);
524+ return result;
525+ }
526+ ```
527+
494528<!-- tabs:end -->
495529
496530<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ char * convert (char * s , int numRows ) {
2+ if (numRows == 1 || numRows >= strlen (s )) {
3+ char * result = malloc (strlen (s ) + 1 );
4+ strcpy (result , s );
5+ return result ;
6+ }
7+ char * * rows = malloc (numRows * sizeof (char * ));
8+ for (int i = 0 ; i < numRows ; i ++ ) {
9+ rows [i ] = malloc (strlen (s ) + 1 );
10+ rows [i ][0 ] = '\0' ;
11+ }
12+ int currentRow = 0 ;
13+ int goingDown = 0 ;
14+ for (int i = 0 ; s [i ] != '\0' ; i ++ ) {
15+ strncat (rows [currentRow ], & s [i ], 1 );
16+ if (currentRow == 0 || currentRow == numRows - 1 ) {
17+ goingDown = !goingDown ;
18+ }
19+ currentRow += goingDown ? 1 : -1 ;
20+ }
21+ char * result = malloc (strlen (s ) + 1 );
22+ result [0 ] = '\0' ;
23+ for (int i = 0 ; i < numRows ; i ++ ) {
24+ strcat (result , rows [i ]);
25+ free (rows [i ]);
26+ }
27+ free (rows );
28+ return result ;
29+ }
You can’t perform that action at this time.
0 commit comments