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 ...
随机推荐
- excel 日期/数字格式不生效需要但双击才会生效的解决办法
原因: Excel2007设置过单元格格式后,并不能立即生效必须挨个双击单元格,才能生效.数据行很多.效率太低. 原因:主要是一些从网上拷贝过来的日期或数字excel默认为文本格式或特殊-中文数字格式 ...
- 深入解析Sqlite的完美替代者,android数据库新王者——Realm
写在前面: 又到一年一度七夕虐狗节,看着大家忍受着各种朋友圈和QQ空间还有现实生活中的轮番轰炸,我实在不忍心再在这里给大家补刀,所以我觉得今天不虐狗,继续给大家分享有用的. 如果你比较关心androi ...
- js学习之变量、作用域和内存问题
js学习之变量.作用域和内存问题 标签(空格分隔): javascript 变量 1.基本类型和引用类型: 基本类型值:Undefined, Null, Boolean, Number, String ...
- 开发者最爱的Firebug停止更新和维护
近日,Firebug团队在其官网上宣布,Firebug将不再继续开发和维护,并邀请大家使用Firefox的内置开发工具. Firebug最初是2006年1月由Joe Hewitt编写, ...
- Ubuntu搭建lnmp环境
1.安装nginx 安装 sudo apt-get install nginx 服务启动.停止.重启 /etc/init.d/nginx start /usr/sbin/nginx -c /etc/n ...
- PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)
最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...
- XAMARIN ANDROID 二维码扫描示例
现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile 做一个简单的 Android 条码扫描示 ...
- 编译器开发系列--Ocelot语言1.抽象语法树
从今天开始研究开发自己的编程语言Ocelot,从<自制编译器>出发,然后再自己不断完善功能并优化. 编译器前端简单,就不深入研究了,直接用现成的一款工具叫JavaCC,它可以生成抽象语法树 ...
- Oracle 列数据聚合方法汇总
网上流传众多列数据聚合方法,现将各方法整理汇总,以做备忘. wm_concat 该方法来自wmsys下的wm_concat函数,属于Oracle内部函数,返回值类型varchar2,最大字符数4000 ...
- win10电脑优化
Windows10必做的优化 --道心 关闭服务 右键点击"此电脑",选择"管理",进入"计算机管理"窗口. 在左侧的菜单选择"服 ...