笔试模拟day1
笔试模拟day1
观前提醒
:
笔试所有系列文章均是记录本人的笔试题思路与代码,所以关于题目的解答,只以我自己能读懂为目标,如果小伙伴们有看不懂的地方,可以评论区留言,我会逐一为大家回复解答哒~ 当然如果有小伙伴们对本文的某些知识或者做题方法有不同的观点或思路,欢迎在评论区一起分享谈论哦o( ̄▽ ̄)ブ
一 回文子串LeetCode链接
二 最长回文子串LeetCode链接
三 分割回文串LeetCode链接
一 回文子串
思路:
class Solution {
public:int countSubstrings(string s) {int n = s.size();vector<vector<bool>> dp(n, vector<bool>(n));int res = 0;for(int i = n-1; i >= 0; i--){for(int j = i; j < n; j++){if(s[i] == s[j]){dp[i][j] = i + 1 < j ? dp[i+1][j-1] : true;}if(dp[i][j]){res++;}}}return res;}
};
二 最长回文子串
思路:
class Solution {
public:string longestPalindrome(string s) {int n = s.size();vector<vector<bool>> dp(n, vector<bool>(n));int len = 1, start = 0;for(int i = n-1; i >= 0; i--){for(int j = i; j < n; j++){if(s[i] == s[j]){dp[i][j] = i + 1 < j?dp[i+1][j-1]:true;}if(dp[i][j] && j-i+1 > len){len = j-i+1;start = i;}}} return s.substr(start,len); }
};
三 分割回文串
思路:
class Solution {
public:bool checkPartitioning(string s) {int n = s.size();vector<vector<bool>> dp(n, vector<bool>(n));for(int i = n-1; i >= 0; i--){for(int j = i; j < n; j++){if(s[i] == s[j]){dp[i][j] = i + 1 < j ?dp[i+1][j-1] : true;}}}for(int i = 1 ; i < n-1; i++){for(int j = i; j < n-1; j++){if(dp[0][i-1] && dp[i][j] && dp[j+1][n-1]){return true;}}}return false;}
};
注:本文的思路都是本人在自己平板的笔记上手写出来的,因为电子屏幕上手写原因字体较差,希望小伙伴们理解~ 如果你们需要更详细的或者更清楚地笔记,欢迎私聊或者评论区留言o( ̄▽ ̄)ブ