Leetcode: Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [
    ["aa","b"],
    ["a","a","b"]
  ]

DFS:

  1. void dfs(string s, vector<string> &path, vector<vector<string>> &res)  
  2.     {  
  3.         if(s.size() < 1)  
  4.         {  
  5.             res.push_back(path);  
  6.             return;  
  7.         }  
  8.         for(int i = 0; i < s.size(); i++)  
  9.         {  
  10.             int begin = 0;  
  11.             int end = i;  
  12.             while(begin < end)  
  13.             {  
  14.                 if(s[begin] == s[end])  
  15.                 {  
  16.                     begin++;  
  17.                     end--;  
  18.                 }  
  19.                 else  
  20.                     break;  
  21.             }  
  22.             if(begin >= end)//bool isPalindrome = true;  
  23.             {  
  24.                 path.push_back(s.substr(0,i+1));  
  25.                 dfs(s.substr(i+1),path,res);  
  26.                 path.pop_back();  
  27.             }  
  28.         }  
  29.     }  
  30.     vector<vector<string>> partition(string s) {  
  31.         // IMPORTANT: Please reset any member data you declared, as  
  32.         // the same Solution instance will be reused for each test case.  
  33.         vector<vector<string>> res;  
  34.         vector<string> path;  
  35.         dfs(s,path,res);  
  36.         return res;  
  37.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值