• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

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

 

posted @ 2013-04-04 03:00  ying_vincent  阅读(230)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3