palindrome-partitioning Java code
2017-10-20 11:32
447 查看
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”,
Return1since the palindrome partitioning[“aa”,”b”]could be produced using 1 cut.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =”aab”,
Return1since the palindrome partitioning[“aa”,”b”]could be produced using 1 cut.
public class Solution { public int minCut(String s) { if (s == null || s.length() <= 1){ return 0; } int n = s.length(); int i; int j; int[] minCut = new int[n + 1]; for (i = 0; i < n + 1; i++){ minCut[i] = i - 1; } for(i = 1; i < n; i++){ // odd-length for (j = 0; i - j >= 0 && i + j < n && s.charAt(i - j) == s.charAt(i + j); j++){ minCut[i + j + 1] = Math.min(minCut[i + j + 1], minCut[i - j] + 1); } // even-length for (j = 0; i - j - 1 >= 0 && i + j < n && s.charAt(i - j - 1) == s.charAt(i + j); j++){ minCut[i + j + 1] = Math.min(minCut[i + j + 1], minCut[i - j - 1] + 1); } } return minCut ; } }
相关文章推荐
- palindrome-partitioning Java code
- 【AtCoder CODE FESTIVAL 2017 qual C】D - Yet Another Palindrome Partitioning (状压dp 字符串)
- Palindrome Partitioning II (Java)
- CODE 1: Palindrome Partitioning II
- leecode 解题总结:132. Palindrome Partitioning II
- Palindrome Partitioning Java
- valid-palindrome Java code
- [leetcode-132]Palindrome Partitioning II(java)
- Palindrome Partitioning II Leetcode Java
- Palindrome Partitioning in Java
- [Atcoder CODE FESTIVAL 2017 qual C]D - Yet Another Palindrome Partitioning 状压DP
- Java for LeetCode 132 Palindrome Partitioning II
- [Leetcode][JAVA] Palindrome Partitioning II
- [leetcode]132. Palindrome Partitioning II(Java)
- [LeetCode][Java] Palindrome Partitioning II
- [Leetcode] Palindrome Partitioning II (Java)
- lintcode: Palindrome Partitioning II
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning
- Palindrome Partitioning II Leetcode java
- Palindrome Partitioning leetcode java