mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
增加0005.最長回文子串C# 雙指針的版本
This commit is contained in:
@ -647,6 +647,36 @@ public class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
雙指針:
|
||||||
|
```C#
|
||||||
|
public class Solution {
|
||||||
|
int maxlenth = 0;
|
||||||
|
int left = 0;
|
||||||
|
int right = 0;
|
||||||
|
|
||||||
|
public string LongestPalindrome(string s) {
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < s.Length; i++) {
|
||||||
|
extend(s, i, i, s.Length); // 以i為中心
|
||||||
|
extend(s, i, i + 1, s.Length); // 以i和i+1為中心
|
||||||
|
}
|
||||||
|
return s.Substring(left, maxlenth);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void extend(string s, int i, int j, int n) {
|
||||||
|
while (i >= 0 && j < n && s[i] == s[j]) {
|
||||||
|
if (j - i + 1 > maxlenth) {
|
||||||
|
left = i;
|
||||||
|
right = j;
|
||||||
|
maxlenth = j - i + 1;
|
||||||
|
}
|
||||||
|
i--;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user