[leetcode-132]Palindrome Partitioning II(java)
来源:互联网 发布:淘宝产品摄影价格表 编辑:程序博客网 时间:2024/06/06 17:53
问题描述:
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.
分析:看完问题之后,我以为会很简单,甚至比上题更简单,直接用DP算法,但是很不幸TLE了。后来仔细分析程序,发现应该是checkPalindrome花费了大量时间,事实上由于这部分的存在,导致时间复杂度变成了O(n3)。看了网友的介绍,发现也是大多在这里做了优化。
首先,如果判断字符串(i,j)为回文,那么充要条件是:chars[i] == chars[j] && (i+1,j-1)是回文。因此,可以利用这个性质,再加一个二维动态规划数组。于是将时间复杂度由O(n3)降为了O(n2)。
代码如下:364ms
public class Solution { public int minCut(String s) { int length = s.length(); if(length<=0) return 0; char[] chars = s.toCharArray(); int[] nums = new int[length]; boolean[][] isValid = new boolean[length][length]; nums[0] = 0; for(int row = 0;row < length;row++) isValid[row][row] = true; for(int i = 1;i<chars.length;i++){ nums[i] = nums[i-1]+1; for(int j = 0;j<i;j++){ if(chars[i] == chars[j]){ if(i == j+1 || isValid[j+1][i-1]){ isValid[j][i] = true; } } if(isValid[j][i]) { if(j==0) nums[i] = 0; else nums[i] = Math.min(nums[i],nums[j-1]+1); } } } return nums[chars.length-1]; }}
代码如下:TLE
private boolean checkPalindrome(String s){ char[] chars = s.toCharArray(); if(chars.length<=0) return false; int start = 0,end = chars.length-1; while(start<=end){ if(chars[start]!=chars[end]){ return false; } start++;end--; } return true; } public int minCut(String s) { int length = s.length(); if(length<=0) return 0; char[] chars = s.toCharArray(); int[] nums = new int[length]; for(int i = 0;i<nums.length;i++) nums[i] = Integer.MAX_VALUE; for(int i = 0;i<chars.length;i++){ for(int j = 0;j<=i;j++){ String s1 = s.substring(j,i+1); if(checkPalindrome(s1)) { if (j - 1 >= 0) { nums[i] = Math.min(nums[i], nums[j - 1] + 1); } else { nums[i] = 0; break; } } } } return nums[chars.length-1]; }
0 0
- [leetcode-132]Palindrome Partitioning II(java)
- [Leetcode] Palindrome Partitioning II (Java)
- Palindrome Partitioning II Leetcode Java
- [LeetCode][Java] Palindrome Partitioning II
- LeetCode: Palindrome Partitioning II [132]
- LeetCode 132 Palindrome Partitioning II
- Leetcode(132) Palindrome Partitioning II
- [leetcode 132] Palindrome Partitioning II
- leetcode || 132、Palindrome Partitioning II
- leetcode 132: Palindrome Partitioning II
- 132 Palindrome Partitioning II [Leetcode]
- LeetCode #132: Palindrome Partitioning II
- Leetcode 132 Palindrome Partitioning II
- LeetCode 132 Palindrome Partitioning II
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- Django 学习笔记(二) 《models》
- 状态码详解
- 10个免费的服务器监控工具
- hdu5371 最长回文子串变形(Manacher算法)
- XmlDocument在C#中的应用
- [leetcode-132]Palindrome Partitioning II(java)
- 编译防火墙——C++的Pimpl惯用法解析
- Notepad++插件emmet快速写html笔记
- Android内存机制分析上篇:了解Android堆和栈
- Android QQ第三方登录(二)
- 魔方玩法入门
- C++集合操作之集合差集:std::set_difference
- linux network stats (cat /proc/net/dev)
- oracle 性能优化建议小结