Palindrome Partitioning II 最小的括号分割
来源:互联网 发布:淘宝运营pdf 编辑:程序博客网 时间:2024/05/17 03:25
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.
前面一道题目是要找到所有的回文子字符组合,现在是找到最小的分割数。很显然,应该用动态规划优化。
开一个数组minValue, minValue[ i ] 存储以 index i 为起点的字符串的最小分割数。
同时开一个二维数组isPal, 用来存储string中子字符串是否为回文字符。( isPal [ i ] [ j ] = isPal [ i ] [ j ] && s [ i ] == s [ j ] )
运行时间:
代码:
public class PalindromePartitioningII { public int minCut(String s) { int[] minCache = new int[s.length()]; boolean[][] isPal = new boolean[s.length()][s.length()]; for (int i = s.length() - 1; i >= 0; i--) { for (int j = i; j < s.length(); j++) { if (( i + 1 > j - 1 || isPal[i + 1][j - 1]) && s.charAt(i)== s.charAt(j)) isPal[i][j] = true; } } Arrays.fill(minCache, Integer.MAX_VALUE); minCache[s.length() - 1] = 0; return doPartition(minCache, isPal, s, 0); } private int doPartition(int[] minCache, boolean[][] isPal, String s, int begin) { if (begin == s.length()) { return 0; } if (minCache[begin] != Integer.MAX_VALUE) { return minCache[begin]; } int curMin = Integer.MAX_VALUE; for (int i = begin; i < s.length(); i++) { if (isPal[begin][i]) { if (i == s.length() - 1) { curMin = 0; break; } curMin = Math.min(curMin, doPartition(minCache, isPal, s, i + 1) + 1); } } minCache[begin] = curMin; return minCache[begin]; }}
1 0
- Palindrome Partitioning II 最小的括号分割
- 最小回文分割数 Palindrome Partitioning II
- 分割成回文需要的最小分割数 Palindrome Partitioning II @LeetCode
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- 动态规划——回文最小分割数(palindrome-partitioning-ii)
- Palindrome Partitioning 回文串分割,求所有可能的分割
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Palindrome Partitioning II
- Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
- ipcrm使用
- Linux系统信息[Linux常用命令笔记]
- 快速建立ssh互信
- 【NOI2014】随机数生成器
- Palindrome Partitioning II 最小的括号分割
- 飛飛(七十五)文件score.dat中保存的是若干名学生的姓名和C++课、高数和英语成绩
- 谈谈口袋助理
- 动态内存分配
- 编译C连接MySQL 提示 error while loading shared libraries: libmysqlclient.so.
- 【NOI2014】购票
- jsp、Html页面注释的种类
- 贝贝网问鼎母婴电商 用户信任是核心资产
- 黑科技/隐性科技展望(外篇)