LeetCode-5LongestPalindromicSubstring(C#)
# 题目
5. Longest Palindromic Substring
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.
# 思路
暴力破解(我和我同学也喜欢叫爆破):
先固定下标,再固定长度,这样就能取出字符串。判断字符串是否是回文串且长度比原来的回文串长,若是,更新,若否,继续取字符串。
// brute force: time O(n ^ 3) space O(n) result: TLE public string LongestPalindrome(string s) { char[] strs = s.ToCharArray(); , end = ; ; i < strs.Length; i++) // start by index i { ; j > i; j--) // end by index j { if (strs[i] == strs[j]) { bool isPalindrome = true; , l = j - ; k < l; k++, l--) // check whether substring is palindrome or not { if (strs[k] != strs[l]) { isPalindrome = false; break; } } if (isPalindrome && j - i > end - start) // compare { start = i; end = j; } } } } ); }
暴力破解,时间复杂度O(n ^ 3),空间复杂度O(n),时间TLE。
我思维有点固化了。总想着先取字符串来判断是否是回文串,其实可以假定它是回文串,看它到底有多长。下面两个方法就是这样思考的。
优化暴力破解:
对于每一个字符,分奇偶,分别尝试去找最长的回文串,并记录长度。
// reference: https://discuss.leetcode.com/topic/23498/very-simple-clean-java-solution // optimize brute force: time O(n ^ 2) space O(n) result: 156ms public void palindrome(char[] strs, int left, int right, ref int start, ref int length) // judge palindrome { && right <= strs.Length - && strs[left] != strs[right]) return; >= && right + <= strs.Length - && strs[left - ] == strs[right + ]) { left--; right++; } ; if (length < newLength) { start = left; length = newLength; } } // optimize brute force : time O(n ^ 2) space O(n) result: public string LongestPalindrome(string s) { ) return s; , length = ; char[] strs = s.ToCharArray(); ; i < strs.Length; i++) { palindrome(strs, i, i, ref start, ref length); // recrusively judge palindrome(strs, i, i + , ref start, ref length); } return s.Substring(start, length); }
优化暴力破解,时间复杂度O(n ^ 2),空间复杂度O(n),时间153ms。
优化遍历:
对于每一个字符,尝试去找最长的回文串,采取以下方法:
1、若是重复串,跳过重复部分(重复串怎么样都是回文串)。
2、非重复串,正常比对头尾。
3、设置下一个字符为非重复部分的下一个字符。
比如baaaaab,遇到第一个a的时候,直接忽略5个a(也就是默认他是回文串了),从b开始尝试寻找回文串。同时下一个需要判断的字符是从第二个b开始。
# 解决(优化遍历)
// reference: https://discuss.leetcode.com/topic/12187/simple-c-solution-8ms-13-lines/ // like cheating method: time O(n ^ 2) space O(n) result: 132ms public string LongestPalindrome(string s) { char[] strs = s.ToCharArray(); , maxLength = , start = ; ) { int k = i, j = i; // j is left, i is middle, k is right && strs[k] == strs[k + ]) k++; // skip duplicate char i = k + ; // set next begin index, we can skip duplicate char && k < s.Length - && strs[j - ] == strs[k + ]) // check palindrome { j--; k++; } ; if (newLength > maxLength) // compare { start = j; maxLength = newLength; } } return s.Substring(start, maxLength); }
优化遍历,时间复杂度O(n ^ 2),空间复杂度O(n),时间132ms。
# 题外话
动态规划也可以做。
具体参考https://discuss.leetcode.com/topic/23498/very-simple-clean-java-solution/12。
状态转移方程:palindrome[i][j] = palindrome[i + 1][j - 1] && s[i] == s[j] 。palindrome[i][j]表示s[i]到s[j]是否是回文串。
题主太懒了,交给你们了。
# 测试用例
static void Main(string[] args) { _5LongestPalindromicSubstring solution = new _5LongestPalindromicSubstring(); Debug.Assert(solution.LongestPalindrome("dddddd") == "dddddd", "wrong 1"); Debug.Assert(solution.LongestPalindrome("abbacdef") == "abba", "wrong 2"); Debug.Assert(solution.LongestPalindrome("cabbadef") == "abba", "wrong 3"); Debug.Assert(solution.LongestPalindrome("cabba") == "abba", "wrong 4"); Debug.Assert(solution.LongestPalindrome("caacbbbbbad") == "bbbbb", "wrong 5"); string veryLong = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"; Debug.Assert(solution.LongestPalindrome(veryLong) == veryLong, "wrong 6"); Debug.Assert(solution.LongestPalindrome("a") == "a", "wrong 7"); Debug.Assert(solution.LongestPalindrome("abb") == "bb", "wrong 8"); }
# 地址
Q: https://leetcode.com/problems/longest-palindromic-substring/
A: https://github.com/mofadeyunduo/LeetCode/blob/master/5LongestPalindromicSubstring/5LongestPalindromicSubstring.cs
(希望各位多多支持本人刚刚建立的GitHub和博客,谢谢,有问题可以邮件609092186@qq.com或者留言,我尽快回复)
LeetCode-5LongestPalindromicSubstring(C#)的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 【CSS进阶】伪元素的妙用--单标签之美
最近在研读 <CSS SECRET>(CSS揭秘)这本大作,对 CSS 有了更深层次的理解,折腾了下面这个项目: CSS3奇思妙想 -- Demo (请用 Chrome 浏览器打开,非常值 ...
- 页面中多个script块之间的关系
一:函数声明与函数定义表达式在函数调用间的区别 <script type="text/javascript"> doA(); var doA = function(a ...
- MVC Core 网站开发(Ninesky) 2.1、栏目的前台显示(补充)
在2.1.栏目的前台显示中因右键没有添加视图把微软给鄙视了一下,后来有仔细研究了一下发现应该鄙视自己,其实这个功能是有的,是自己没搞清楚乱吐糟. 其实只要在NuGet中安装两个包(Microsoft. ...
- 用scikit-learn学习DBSCAN聚类
在DBSCAN密度聚类算法中,我们对DBSCAN聚类算法的原理做了总结,本文就对如何用scikit-learn来学习DBSCAN聚类做一个总结,重点讲述参数的意义和需要调参的参数. 1. scikit ...
- [C#] 了解过入口函数 Main() 吗?带你用批处理玩转 Main 函数
了解过入口函数 Main() 吗?带你用批处理玩转 Main 函数 目录 简介 特点 方法的参数 方法的返回值 与批处理交互的一个示例 简介 我们知道,新建一个控制台应用程序的时候,IDE 会同时创建 ...
- 企业做数据缓存是使用Memcached还是选Redis?
企业是使用Memcached还是选Redis? 在构建一款现代且由数据库驱动的Web应用程序并希望使其拥有更为出色的性能表现时,这个问题总会时不时出现.并给每一位开发人员带来困扰.在考虑对应用程序的性 ...
- maven依赖查询地址
http://search.maven.org/#search%7Cga%7C1%7C
- 图解Spark API
初识spark,需要对其API有熟悉的了解才能方便开发上层应用.本文用图形的方式直观表达相关API的工作特点,并提供了解新的API接口使用的方法.例子代码全部使用python实现. 1. 数据源准备 ...
- Win10连接远程桌面时提示“您的凭据不工作”
我遇到这个问题的时候查找网上都给出一堆高大上的解决办法, 然而我的错误实际上是用户名的问题, 很多人以为远程用户名就一定是锁屏状态下的登录名, 其实不是,跟自己设置有关,所以首先应该检查远程用户名是否 ...
- 2DToolkit官方文档中文版打地鼠教程(三):Sprite Collections 精灵集合
这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...