File tree Expand file tree Collapse file tree 6 files changed +222
-1
lines changed
0005.Longest Palindromic Substring Expand file tree Collapse file tree 6 files changed +222
-1
lines changed Original file line number Diff line number Diff line change @@ -277,6 +277,42 @@ public class Solution {
277
277
}
278
278
```
279
279
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
+
280
316
#### Nim
281
317
282
318
```nim
Original file line number Diff line number Diff line change @@ -275,6 +275,42 @@ public class Solution {
275
275
}
276
276
```
277
277
278
+ #### C
279
+
280
+ ``` c
281
+ char * longestPalindrome (char* s) {
282
+ int n = strlen(s);
283
+ bool** f = (bool** ) malloc(n * sizeof(bool* ));
284
+ for (int i = 0; i < n; ++i) {
285
+ f[ i] = (bool* ) malloc(n * sizeof(bool));
286
+ for (int j = 0; j < n; ++j) {
287
+ f[ i] [ j ] = true;
288
+ }
289
+ }
290
+ int k = 0, mx = 1;
291
+ for (int i = n - 2; ~ i; --i) {
292
+ for (int j = i + 1; j < n; ++j) {
293
+ f[ i] [ j ] = false;
294
+ if (s[ i] == s[ j] ) {
295
+ f[ i] [ j ] = f[ i + 1] [ j - 1 ] ;
296
+ if (f[ i] [ j ] && mx < j - i + 1) {
297
+ mx = j - i + 1;
298
+ k = i;
299
+ }
300
+ }
301
+ }
302
+ }
303
+ char* res = (char* ) malloc((mx + 1) * sizeof(char));
304
+ strncpy(res, s + k, mx);
305
+ res[ mx] = '\0';
306
+ for (int i = 0; i < n; ++i) {
307
+ free(f[ i] );
308
+ }
309
+ free(f);
310
+ return res;
311
+ }
312
+ ```
313
+
278
314
#### Nim
279
315
280
316
```nim
Original file line number Diff line number Diff line change
1
+ char * longestPalindrome (char * s ) {
2
+ int n = strlen (s );
3
+ bool * * f = (bool * * ) malloc (n * sizeof (bool * ));
4
+ for (int i = 0 ; i < n ; ++ i ) {
5
+ f [i ] = (bool * ) malloc (n * sizeof (bool ));
6
+ for (int j = 0 ; j < n ; ++ j ) {
7
+ f [i ][j ] = true;
8
+ }
9
+ }
10
+ int k = 0 , mx = 1 ;
11
+ for (int i = n - 2 ; ~i ; -- i ) {
12
+ for (int j = i + 1 ; j < n ; ++ j ) {
13
+ f [i ][j ] = false;
14
+ if (s [i ] == s [j ]) {
15
+ f [i ][j ] = f [i + 1 ][j - 1 ];
16
+ if (f [i ][j ] && mx < j - i + 1 ) {
17
+ mx = j - i + 1 ;
18
+ k = i ;
19
+ }
20
+ }
21
+ }
22
+ }
23
+ char * res = (char * ) malloc ((mx + 1 ) * sizeof (char ));
24
+ strncpy (res , s + k , mx );
25
+ res [mx ] = '\0' ;
26
+ for (int i = 0 ; i < n ; ++ i ) {
27
+ free (f [i ]);
28
+ }
29
+ free (f );
30
+ return res ;
31
+ }
Original file line number Diff line number Diff line change @@ -282,6 +282,47 @@ public class Solution {
282
282
}
283
283
```
284
284
285
+ #### C
286
+
287
+ ``` c
288
+ char * convert (char* s, int numRows) {
289
+ if (numRows == 1) {
290
+ return strdup(s);
291
+ }
292
+
293
+ int len = strlen(s);
294
+ char** g = (char**) malloc(numRows * sizeof(char*));
295
+ int* idx = (int*) malloc(numRows * sizeof(int));
296
+ for (int i = 0; i < numRows; ++i) {
297
+ g[i] = (char*) malloc((len + 1) * sizeof(char));
298
+ idx[i] = 0;
299
+ }
300
+
301
+ int i = 0, k = -1;
302
+ for (int p = 0; p < len; ++p) {
303
+ g[i][idx[i]++] = s[p];
304
+ if (i == 0 || i == numRows - 1) {
305
+ k = -k;
306
+ }
307
+ i += k;
308
+ }
309
+
310
+ char* ans = (char*) malloc((len + 1) * sizeof(char));
311
+ int pos = 0;
312
+ for (int r = 0; r < numRows; ++r) {
313
+ for (int j = 0; j < idx[r]; ++j) {
314
+ ans[pos++] = g[r][j];
315
+ }
316
+ free(g[r]);
317
+ }
318
+ ans[pos] = '\0';
319
+
320
+ free(g);
321
+ free(idx);
322
+ return ans;
323
+ }
324
+ ```
325
+
285
326
<!-- tabs:end -->
286
327
287
328
<!-- solution:end -->
@@ -491,7 +532,7 @@ class Solution {
491
532
return $result;
492
533
}
493
534
}
494
- ```
535
+ ``
495
536
496
537
<!-- tabs:end -->
497
538
Original file line number Diff line number Diff line change @@ -280,6 +280,47 @@ public class Solution {
280
280
}
281
281
```
282
282
283
+ #### C
284
+
285
+ ``` c
286
+ char * convert (char* s, int numRows) {
287
+ if (numRows == 1) {
288
+ return strdup(s);
289
+ }
290
+
291
+ int len = strlen(s);
292
+ char** g = (char**) malloc(numRows * sizeof(char*));
293
+ int* idx = (int*) malloc(numRows * sizeof(int));
294
+ for (int i = 0; i < numRows; ++i) {
295
+ g[i] = (char*) malloc((len + 1) * sizeof(char));
296
+ idx[i] = 0;
297
+ }
298
+
299
+ int i = 0, k = -1;
300
+ for (int p = 0; p < len; ++p) {
301
+ g[i][idx[i]++] = s[p];
302
+ if (i == 0 || i == numRows - 1) {
303
+ k = -k;
304
+ }
305
+ i += k;
306
+ }
307
+
308
+ char* ans = (char*) malloc((len + 1) * sizeof(char));
309
+ int pos = 0;
310
+ for (int r = 0; r < numRows; ++r) {
311
+ for (int j = 0; j < idx[r]; ++j) {
312
+ ans[pos++] = g[r][j];
313
+ }
314
+ free(g[r]);
315
+ }
316
+ ans[pos] = '\0';
317
+
318
+ free(g);
319
+ free(idx);
320
+ return ans;
321
+ }
322
+ ```
323
+
283
324
<!-- tabs:end -->
284
325
285
326
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ char * convert (char * s , int numRows ) {
2
+ if (numRows == 1 ) {
3
+ return strdup (s );
4
+ }
5
+
6
+ int len = strlen (s );
7
+ char * * g = (char * * ) malloc (numRows * sizeof (char * ));
8
+ int * idx = (int * ) malloc (numRows * sizeof (int ));
9
+ for (int i = 0 ; i < numRows ; ++ i ) {
10
+ g [i ] = (char * ) malloc ((len + 1 ) * sizeof (char ));
11
+ idx [i ] = 0 ;
12
+ }
13
+
14
+ int i = 0 , k = -1 ;
15
+ for (int p = 0 ; p < len ; ++ p ) {
16
+ g [i ][idx [i ]++ ] = s [p ];
17
+ if (i == 0 || i == numRows - 1 ) {
18
+ k = - k ;
19
+ }
20
+ i += k ;
21
+ }
22
+
23
+ char * ans = (char * ) malloc ((len + 1 ) * sizeof (char ));
24
+ int pos = 0 ;
25
+ for (int r = 0 ; r < numRows ; ++ r ) {
26
+ for (int j = 0 ; j < idx [r ]; ++ j ) {
27
+ ans [pos ++ ] = g [r ][j ];
28
+ }
29
+ free (g [r ]);
30
+ }
31
+ ans [pos ] = '\0' ;
32
+
33
+ free (g );
34
+ free (idx );
35
+ return ans ;
36
+ }
You can’t perform that action at this time.
0 commit comments