leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0005.cpp (591B)
0 class Solution { 1 const string len(const string &s, int a, int b) { 2 while (a >= 0 && b < s.size() && s[a] == s[b]) 3 a--, b++; 4 return s.substr(a + 1, b - a - 1); 5 } 6 7 public: 8 string longestPalindrome(string s) { 9 string res = "", t; 10 s.push_back(' '); 11 for (int i = 0; i < s.size(); i++) { 12 t = len(s, i, i); 13 if (t.size() > res.size()) res = t; 14 if (s[i] != s[i + 1]) continue; 15 t = len(s, i, i + 1); 16 if (t.size() > res.size()) res = t; 17 } 18 return res; 19 } 20 };