LeetCode: Longest Palindromic Substring

自己的large没过,找到的网上答案跟一开始的想法是一样的,不过被网上的一个string的substr用法给坑了。。string.substr(strposstart, length)

 1 class Solution {

 2 public:

 3     string longestPalindrome(string s) {

 4         // Start typing your C/C++ solution below

 5         // DO NOT write int main() function

 6         if (s == "" || s.length() <= 1) return s;  

 7         string longestPalindrome = "";  

 8         for (int i = 0; i < s.length() - 1; i++) {  

 9             string palindrome = getLongestPalindrome(s, i, i);  

10             if (palindrome.length() > longestPalindrome.length()) {  

11                 longestPalindrome = palindrome;  

12             }  

13               

14             palindrome = getLongestPalindrome(s, i, i + 1);  

15             if (palindrome.length() > longestPalindrome.length()) {  

16                 longestPalindrome = palindrome;  

17             }   

18         }  

19         return longestPalindrome;  

20     }

21     string getLongestPalindrome(string s, int l, int r) {  

22         int length = s.length();  

23         while(l >= 0 && r < length && s[l] == s[r]) {  

24             l--;  

25             r++;  

26         }  

27         return s.substr(l + 1, r - l - 1);  

28     }  

29 };

 下面这段代码更好

 1 class Solution {

 2 public:

 3     string longestPalindrome(string s) {

 4         // IMPORTANT: Please reset any member data you declared, as

 5         // the same Solution instance will be reused for each test case.

 6         string res;

 7         for (int i = 0; i < s.size(); i++) {

 8             int left, right;

 9             left = right = i;

10             while (left >= 0 && right < s.size() && s[left] == s[right]) {

11                 left--;

12                 right++;

13             }

14             if (right - 1 - (left + 1) + 1 > res.size()) res = s.substr(left+1, right-left-1);

15             left = i, right = i+1;

16             while (left >= 0 && right < s.size() && s[left] == s[right]) {

17                 left--;

18                 right++;

19             }

20             if (right - left - 1 > res.size()) res = s.substr(left+1, right-left-1);

21         }

22         return res;

23     }

24 };

 C#

 1 public class Solution {

 2     public string LongestPalindrome(string s) {

 3         string ans = "";

 4         for (int i = 0; i < s.Length; i++) {

 5             int left = i, right = i;

 6             while (left >= 0 && right < s.Length && s[left] == s[right]) {

 7                 left--;

 8                 right++;

 9             }

10             if (right - left - 1 > ans.Length) ans = s.Substring(left + 1, right - left - 1);

11             left = i; right = i + 1;

12             while (left >= 0 && right < s.Length && s[left] == s[right]) {

13                 left--;

14                 right++;

15             }

16             if (right - left - 1 > ans.Length) ans = s.Substring(left + 1, right - left  - 1);

17         }

18         return ans;

19     }

20 }
View Code

 

你可能感兴趣的:(substring)