首页 > 代码库 > [leetcode]Palindrome Partitioning II
[leetcode]Palindrome Partitioning II
问题描述:
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 ofs.
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。