leetCode:Longest Palindromic Substring
2014-09-25 13:15
447 查看
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of
S is 1000, and there exists one unique longest palindromic substring
S is 1000, and there exists one unique longest palindromic substring
#include<iostream> #include<string> using namespace std; class Solution{ public: string longestPalindrome1(string s); }; string Solution::longestPalindrome1(string s){ int start=0; int maxlength=1; string res; if(s.length()==1) return s; int low,high; for(int i=1;i<s.length();++i){ low = i - 1; high = i; while (low >= 0 && high < s.length() && s[low] == s[high]){ if (high - low + 1 > maxlength){ start = low; maxlength = high - low + 1; } --low; ++high; } low = i - 1; high = i + 1; while (low >= 0 && high <s.length() && s[low] == s[high]) { if (high - low + 1 > maxlength) { start = low; maxlength = high - low + 1; } --low; ++high; } res = s.substr(start,maxlength); return res; } }
相关文章推荐
- LeetCode - Longest Palindromic Substring 题解
- LeetCode--Longest Palindromic Substring
- leetcode——Longest Palindromic Substring
- 【LeetCode】 005. Longest Palindromic Substring
- LeetCode: Longest Palindromic Substring
- leetcode 5:Longest Palindromic Substring 三种解法的java实现源代码,以及详细讲解
- [Leetcode] Longest Palindromic Substring
- LeetCode 5 : Longest Palindromic Substring(C++)
- LeetCode 5 Longest Palindromic Substring
- LeetCode----Longest Palindromic Substring
- LeetCode第五关:Longest Palindromic Substring --> Difficulty: Medium
- LeetCode: Longest Palindromic Substring
- LeetCode 第五题 Longest Palindromic Substring
- LeetCode-5-Longest Palindromic Substring(C语言实现)
- leetcode Longest Palindromic Substring
- [LeetCode] 045: Longest Palindromic Substring
- leetcode Longest Palindromic Substring
- leetcode:5. Longest Palindromic Substring
- [LeetCode] Longest Palindromic Substring
- 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】