标签:set nts public nbsp tco rom 字符 string count
problem:https://leetcode.com/problems/palindromic-substrings/
对于每个字符,找到前面和它匹配的字符,再看中间夹着的字符串是不是len - 2的回文串,如果是,结果加一。
class Solution { public: int countSubstrings(string s) { int res = s.size(); vector<int> dp(2 * s.size() - 1); for (int i = 0; i < dp.size(); i++) { if ((i % 2) == 0) dp[i] = 1; else dp[i] = 0; } vector<unordered_set<int>> nums(256); for (int i = 0; i < s.size(); i++) // end { for(auto& j : nums[s[i]]) { int len = i - j + 1; if (dp[i + j] == len - 2) { dp[i + j] = len; res++; } } nums[s[i]].insert(i); } return res; } };
[动态规划] leetcode 647 Palindromic Substrings
标签:set nts public nbsp tco rom 字符 string count
原文地址:https://www.cnblogs.com/fish1996/p/11323637.html