lintode(108)分割回文串 II
来源:互联网 发布:js里能生成uuid吗 编辑:程序博客网 时间:2024/05/17 23:12
描述:
给定一个字符串s,将s分割成一些子串,使每个子串都是回文。
返回s符合要求的的最少分割次数。
样例:
比如,给出字符串s = "aab",
返回 1, 因为进行一次分割可以将字符串s分割成["aa","b"]这样两个回文子串
思路:
eg:aabbaac
首先确定从s[i]到s[j]是否为回字符串
aabbaacaTTFFFTFa TFFTFFb TTFFFb TFFFa TTFa TFc T然后统计最小分割次数:
0123456result1322110public class Solution { /** * @param s a string * @return an integer */ public int minCut(String s) { // write your code here if(s == null || s.length() == 0){ return 0; } int len = s.length(); int[] result = new int[len]; boolean[][] whether = new boolean[len][len]; search(whether , s); for(int i = len - 1;i>=0;i--){ if(whether[i][len - 1]){ result[i] = 0; continue; } result[i] = len - 1; for(int j = i+1;j<len;j++){ if(whether[i][j - 1]){ result[i] = Math.min(result[i] , result[j] + 1); } } } return result[0]; } public void search(boolean[][] whether , String s){ for(int l = 1 ; l<= s.length();l++){ for(int i = 0;i <= s.length() - l;i++){ int j = i + l -1; if(l == 1){ whether[i][j] = true; }else if(l == 2){ whether[i][j] = (s.charAt(i) == s.charAt(j)); }else{ whether[i][j] = (s.charAt(i) == s.charAt(j)) && whether[i+1][j-1]; } } } }};
0 0
- lintode(108)分割回文串 II
- 分割回文串II
- 分割回文串 II
- LintCode-分割回文串 II
- 题目:分割回文串 II
- LintCode: 分割回文串 II
- 分割回文串 II-LintCode
- 最小回文分割数 Palindrome Partitioning II
- 108.Palindrome Partitioning II-分割回文串 II(中等题)
- 题目:分割回文串
- LintCode- 分割回文串
- 分割回文串
- LintCode:分割回文串
- 分割回文串
- 分割回文串
- 分割回文串
- 分割回文串I
- 分割回文串-LintCode
- Unity网络通信与端口通信
- 技术转型产品学习笔记与理解[MRD]简述——第四天
- JavaScript test() 方法
- java基础总结14-面向对象10(多态)
- Docker for Windows 中文文档(2)——Set up tab completion in PowerShell
- lintode(108)分割回文串 II
- pymongo 存取
- Matlab编程 二维图形绘制进阶
- android开发中mvp与mvc的差别,如何使mvp(demo)
- 数据准备-在编码格式上摔的坑
- ubuntu如何安装jenkins
- C# 网页sapx页面调用用户邮箱客户端
- 树和二叉树
- 简单的递归题目