File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed
solution/0000-0099/0005.Longest Palindromic Substring Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -456,6 +456,39 @@ impl Solution {
456456}
457457```
458458
459+ #### C#
460+
461+ ``` cs
462+ public class Solution {
463+ private string s ;
464+ private int n ;
465+
466+ public String LongestPalindrome (string s ) {
467+ this .s = s ;
468+ n = s .Length ;
469+ int start = 0 , mx = 1 ;
470+ for (int i = 0 ; i < n ; ++ i ) {
471+ int a = F (i , i );
472+ int b = F (i , i + 1 );
473+ int t = Math .Max (a , b );
474+ if (mx < t ) {
475+ mx = t ;
476+ start = i - ((t - 1 ) >> 1 );
477+ }
478+ }
479+ return s .Substring (start , start + mx );
480+ }
481+
482+ private int F (int l , int r ) {
483+ while (l >= 0 && r < n && s [l ] == s [r ]) {
484+ -- l ;
485+ ++ r ;
486+ }
487+ return r - l - 1 ;
488+ }
489+ }
490+ ```
491+
459492#### PHP
460493
461494``` php
Original file line number Diff line number Diff line change @@ -454,6 +454,39 @@ impl Solution {
454454}
455455```
456456
457+ #### C#
458+
459+ ``` cs
460+ public class Solution {
461+ private string s ;
462+ private int n ;
463+
464+ public String LongestPalindrome (string s ) {
465+ this .s = s ;
466+ n = s .Length ;
467+ int start = 0 , mx = 1 ;
468+ for (int i = 0 ; i < n ; ++ i ) {
469+ int a = F (i , i );
470+ int b = F (i , i + 1 );
471+ int t = Math .Max (a , b );
472+ if (mx < t ) {
473+ mx = t ;
474+ start = i - ((t - 1 ) >> 1 );
475+ }
476+ }
477+ return s .Substring (start , start + mx );
478+ }
479+
480+ private int F (int l , int r ) {
481+ while (l >= 0 && r < n && s [l ] == s [r ]) {
482+ -- l ;
483+ ++ r ;
484+ }
485+ return r - l - 1 ;
486+ }
487+ }
488+ ```
489+
457490#### PHP
458491
459492``` php
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ private string s ;
3+ private int n ;
4+
5+ public String LongestPalindrome ( string s ) {
6+ this . s = s ;
7+ n = s . Length ;
8+ int start = 0 , mx = 1 ;
9+ for ( int i = 0 ; i < n ; ++ i ) {
10+ int a = F ( i , i ) ;
11+ int b = F ( i , i + 1 ) ;
12+ int t = Math . Max ( a , b ) ;
13+ if ( mx < t ) {
14+ mx = t ;
15+ start = i - ( ( t - 1 ) >> 1 ) ;
16+ }
17+ }
18+ return s . Substring ( start , start + mx ) ;
19+ }
20+
21+ private int F ( int l , int r ) {
22+ while ( l >= 0 && r < n && s [ l ] == s [ r ] ) {
23+ -- l ;
24+ ++ r ;
25+ }
26+ return r - l - 1 ;
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments