标签:tco round 回文 substr 动态 http problems 规划 --
题目
示例一
解题
回文:字符串正向读和反向读是一样就叫回文
根据上面规律,想到用动态规划解决这个问题
public string LongestPalindrome(string s) { if (string.IsNullOrEmpty(s) || s.Length <= 1) return s; var result = string.Empty; var dp = new bool[s.Length, s.Length]; for (int i = s.Length - 1; i >= 0; i--) { for (int k = i; k < s.Length; k++) { dp[i, k] = s[i] == s[k] && (k - i < 2 || dp[i + 1, k - 1]); if (dp[i, k] && (k - i + 1) > result.Length) { result = s.Substring(i, k - i + 1); } } } return result; }
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-palindromic-substring/submissions
标签:tco round 回文 substr 动态 http problems 规划 --
原文地址:https://www.cnblogs.com/WilsonPan/p/11730899.html