码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]Palindrome Partitioning II

时间:2015-01-26 11:53:51      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:leetcode   算法   

问题描述:

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

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.


代码:

    int minCut(string s) {  //C++
        int len = s.size();
        if(len == 0)
            return  0;
        
        vector<int> nodes(len+1);
        for(int i = 0; i<=len; i++)
            nodes[i]=i-1;
            
        for(int i = 0; i<len; i++){
            for(int j = 0; i+j<len&&i-j>=0&&s[i+j]==s[i-j] ;j++ )
                nodes[i+j+1] = min(nodes[i+j+1],nodes[i-j]+1);
            for(int j = 0; i+j-1<len&&i-j>=0&&s[i+j-1]==s[i-j] ;j++ )
                nodes[i+j] = min(nodes[i+j],nodes[i-j]+1);
        }
        return nodes[len];
    }


[leetcode]Palindrome Partitioning II

标签:leetcode   算法   

原文地址:http://blog.csdn.net/chenlei0630/article/details/43150467

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!