您的位置:首页 > 其它

Problem I : Palindrome Partitioning I

2016-07-22 10:00 211 查看
Problem I : Palindrome Partitioning I 

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"]
]


Solve the problem on Leetcode 
class Solution {

public:

    vector<vector<string>> partition(string s)

    {

        vector<vector<string>> res;

        vector<string> tmp;

        dfs(res,tmp,s);

        return res;

    }

    

    void dfs(vector<vector<string>> &res,vector<string> &tmp,string s)

    {//截成多少段

        if(s.length()==0)

        {

            res.push_back(tmp);

            return ;

        }

       

       

        for(int i=1;i<=s.length();i++)

        {

            //md 理解substr函数吗

          string  sub_str=s.substr(0,i);

            if(isvaild(sub_str))

            {

               tmp.push_back(sub_str);

               dfs(res,tmp,s.substr(i));

               tmp.pop_back();

            }

        }

        

    }

     bool isvaild(string s)

    {

        int i=0;

        int j=s.length()-1;

        while(i<j)

        {

            if(s.at(i++)!=s.at(j--)) return false;

        }

        return true;

            

    }

    };

1 dfs 理解
2 字串substr函数
3 回文数字
参考:http://blog.csdn.net/u011095253/article/details/9177451
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: