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 ...
随机推荐
- .net core快速上手
2014年11月12日的Connect ();开发者活动上宣布将.NET堆栈基于MIT协议开源,并且提供开源保证,托管在Github上.当时的版本与最终目标相距甚远,然而有一点可以肯定的是,这是一个与 ...
- 0-1背包问题蛮力法求解(java版本)
sloves: package BackPack; public class Solves { public int[] DecimaltoBinary(int n,int m) { int ...
- 2.WindowsServer2012R2装完的一些友好化设置
网站部署之~Windows Server | 本地部署 http://www.cnblogs.com/dunitian/p/4822808.html#iis 1.桌面图标(控制面板里面屏蔽了,得自己输 ...
- JavaScript的继承实现方式
1.使用call或apply方法,将父对象的构造函数绑定在子对象上 function A(){ this.name = 'json'; } function B(){ A.call(this); } ...
- 前端学HTTP之日志记录
前面的话 几乎所有的服务器和代理都会记录下它们所处理的HTTP事务摘要.这么做出于一系列的原因:跟踪使用情况.安全性.计费.错误检测等等.本文将谥介绍日志记录 记录内容 大多数情况下,日志的记录出于两 ...
- 【原创分享·微信支付】C# MVC 微信支付教程系列之现金红包
微信支付教程系列之现金红包 最近最弄这个微信支付的功能,然后扫码.公众号支付,这些都做了,闲着无聊,就看了看微信支付的其他功能,发现还有一个叫“现金红包”的玩意,想 ...
- C#中Length和Count的区别(个人观点)
这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...
- C#语法糖大汇总
首先需要声明的是"语法糖"这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕 ...
- 页面布局class常见命名规范
头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:left rig ...
- MySQL常用命令
数据库登陆命令: mysql -uroot -p 2.提示输入密码: 3.登陆成功: 4.数据库修改相关命令: 修改数据库的编码格式: 语法格式为:ALTER {DATABASE|SCHEMA} [ ...