LeetCode 5 Longest Palindromic Substring(最大回文子字符串)
2015-09-20 13:46
525 查看
翻译
原文
暴力搜索,O(n3)O(n^3)
动态规划,时间:O(n2)O(n^2),空间:O(n2)O(n^2)
然而,继续悲剧……
好吧,这种方法来源于网络……据说要O(n2)O(n^2)的时间,但仅要O(1)O(1)的空间!
哎,继续努力了!
给定一个字符串S,找出它的最大回文子字符串。 你可以假定S的最大长度为1000, 并且这里存在唯一一个最大回文子字符串。
原文
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.
暴力搜索,O(n3)O(n^3)
public static string LongestPalindrome(string s) { int len = s.Length; for (int i = len; i > 0; i--) for (int j = 1; j <= len + 1 - i; j++) if (isPalindrome(s.Substring(j - 1, i))) return s.Substring(j - 1, i); return null; } public static bool isPalindrome(string s) { for (int i = 0; i < s.Length / 2; i++) if (s.Substring(i, 1) != s.Substring(s.Length - 1 - i, 1)) return false; return true; }
动态规划,时间:O(n2)O(n^2),空间:O(n2)O(n^2)
public class Solution { public string LongestPalindrome(string s) { int sLen = s.Length; int lonBeg = 0; int maxLen = 1; bool[,] DP = new bool[1000, 1000]; for (int i = 0; i < sLen; i++) { DP[i, i] = true; } for (int i = 0; i < sLen - 1; i++) { if (s[i] == s[i + 1]) { DP[i, i + 1] = true; lonBeg = i; maxLen = 2; } } for (int len = 3; len <= sLen; len++) // 字符串长度从3开始的所有子字符串 { for (int i = 0; i < sLen + 1 - len; i++) { int j = len - 1 + i; // j为数组尾部的索引 if (s[i] == s[j] && DP[i + 1, j - 1]) { DP[i, j] = true; // i到j为回文 lonBeg = i; // lonBeg为起始索引,等于i maxLen = len; // maxLen为字符串长度 } } } return s.Substring(lonBeg, maxLen); } }
然而,继续悲剧……
Submission Result: Memory Limit Exceeded
public class Solution { public string LongestPalindrome(string s) { int nLen = s.Length; if (nLen == 0) return ""; string lonStr = s.Substring(0, 1); for (int i = 0; i < nLen - 1; i++) { string p1 = ExpandAroundCenter(s, i, i); if (p1.Length > lonStr.Length) lonStr = p1; string p2 = ExpandAroundCenter(s, i, i + 1); if (p2.Length > lonStr.Length) lonStr = p2; } return lonStr; } public static string ExpandAroundCenter(string s, int n, int m) { int l = n, r = m; int nLen = s.Length; while (l >= 0 && r <= nLen - 1 && s[l] == s[r]) { l--; r++; } return s.Substring(l + 1, r - l - 1); } }
好吧,这种方法来源于网络……据说要O(n2)O(n^2)的时间,但仅要O(1)O(1)的空间!
哎,继续努力了!
相关文章推荐
- SSO单点登录
- Python 类 --基础与要点
- MVC 异步请求
- vim中同时打开多个文件 同时显示多个窗口
- MySQL数据库
- 在PPT2007中制作礼花绽放动画效果ppt模板打包下载
- python获取对象信息type、dir、getattr、setattr
- 二十9天 月出冲击黑鸟 —Spring的AOP_AspectJ @annotation
- 【Andord真】SlideMenu+ViewPagerIndictor双滑动边栏+滑动导航条
- android之模拟手机助手下载
- 使用java语言向oracle数据库中存入、取出图片及文件
- 杭电2212DFS
- testlink的操作过程
- Android轻量级ORM框架ActiveAndroid入门教程(转)
- HDU 5463Clarke and minecraft
- 杰帝电影收藏
- 获取SD卡的剩余容量
- asp.net MVC 通用登录验证模块
- Ubuntu12.04下hadoop-2.6.0单机配置和伪分布式配置
- js == 和 ===