wordpress 删除站点,网站备案之后,网站恶意点击软件,wordpress商品列表131. 分割回文串https://leetcode.cn/problems/palindrome-partitioning/https://leetcode.cn/problems/palindrome-partitioning/
给你一个字符串 s#xff0c;请你将 s 分割成一些子串#xff0c;使每个子串都是 回文串 。返回 s 所有可能的分割方案。
示例 1#xff1a…131. 分割回文串https://leetcode.cn/problems/palindrome-partitioning/https://leetcode.cn/problems/palindrome-partitioning/
给你一个字符串 s请你将 s 分割成一些子串使每个子串都是 回文串 。返回 s 所有可能的分割方案。
示例 1
输入s aab
输出[[a,a,b],[aa,b]]示例 2
输入s a
输出[[a]]提示
1 s.length 16s 仅由小写英文字母组成
DFS方案1初始化一个代表分割点的01数组然后对这个数组的状态使用dfs搜索。这个方案耗时长空间复杂度也不小。具体代码如下
class Solution {
public:vectorvectorstring ans;bool cutoff[20] {false};//代表分割点的01数组bool check(string s)//判断回文串{string ss s;reverse(s.begin(),s.end());if(sss)return true;else return false;}void dfs(string s,int cur)//枚举每一个位置有在该点分割与不分割两种情况{if(cur s.size()){vectorstring temp;int i 0;while(true){string t;t.push_back(s[i]);i;while(!cutoff[i]){t.push_back(s[i]);i;}if(!check(t))return;else temp.push_back(t);if (i s.size())break;}ans.push_back(temp);return;}cutoff[cur] true;dfs(s,cur1);cutoff[cur] false;dfs(s,cur1);}vectorvectorstring partition(string s) {//为了整体代码和谐将首尾都设为分割cutoff[0] true;cutoff[s.size()] true;dfs(s,1);return ans;}
}; DFS方案2从前到后直接枚举分割点。如下图
这个方案更快些具体差别在该方案能及时排除不可能选项而不是等上一个方案把所有可能情况全列出来然后对每个依次排除。代码具体如下
class Solution {
public:vectorvectorstring ans;vectorstring temp;bool check(string s){string ss s;reverse(s.begin(),s.end());if(sss)return true;else return false;}void dfs(int startindex,const string s)//前者代表起始点{if(startindexs.size()){ans.push_back(temp);return;}for(int i startindex;is.size();i){string str s.substr(startindex,i-startindex 1);if(check(str)){temp.push_back(str);dfs(i1,s);temp.pop_back();}else continue;}}vectorvectorstring partition(string s) {dfs(0,s);return ans;}
};
顺便说一道几乎一样的题
93. 复原 IP 地址https://leetcode.cn/problems/restore-ip-addresses/
有效 IP 地址 正好由四个整数每个整数位于 0 到 255 之间组成且不能含有前导 0整数之间用 . 分隔。
例如0.1.2.201 和 192.168.1.1 是 有效 IP 地址但是 0.011.255.245、192.168.1.312 和 192.1681.1 是 无效 IP 地址。
给定一个只包含数字的字符串 s 用以表示一个 IP 地址返回所有可能的有效 IP 地址这些地址可以通过在 s 中插入 . 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。
示例 1
输入s 25525511135
输出[255.255.11.135,255.255.111.35]示例 2
输入s 0000
输出[0.0.0.0]示例 3
输入s 101023
输出[1.0.10.23,1.0.102.3,10.1.0.23,10.10.2.3,101.0.2.3]提示
1 s.length 20s 仅由数字组成
思路几乎一致都是要求分割的字符串所有情况唯一不同的是这道题有些额外的条件但都很好解决具体代码如下
class Solution {
public:vectorstring ans;vectorstring temp;inline int transform(string s)//字符串转数字{int t 0;int re 0;for(int i s.size()-1;i0;i--,t){re(s[i] - 0)*pow(10,t);}return re;}bool check(string s){string ss s;reverse(s.begin(),s.end());if(sss)return true;else return false;}void dfs(int startindex,const string s)//前者代表起始点{if(temp.size()4)//很明显的剪枝return;if(startindexs.size()temp.size()4){string str;for(int i 0;itemp.size();i){for(int j 0;jtemp[i].size();j){str.push_back(temp[i][j]);}str.push_back(.);}str.pop_back();ans.push_back(str);return;}for(int i startindex;is.size();i){string str s.substr(startindex,i-startindex 1);if((str.size()2str[0] 0)||str.size()3||transform(str)255)//由题得出的筛选条件continue;temp.push_back(str);dfs(i1,s);temp.pop_back();}}vectorstring restoreIpAddresses(string s) {dfs(0,s);return ans;}
};
文章转载自: http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.mwjwy.cn.gov.cn.mwjwy.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.fqlxg.cn.gov.cn.fqlxg.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.njqpg.cn.gov.cn.njqpg.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.gctgc.cn.gov.cn.gctgc.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn