Palindrome Partitioning I II@LeetCode

Palindrome Partitioning

````java````public class Solution {
public List<List<String>> partition(String s) {
return result;
}

private void generate(List<List<String>> result, LinkedList<String> list, String s) {
if (s.length() == 0) {
return;
}
int length = s.length();
for (int i = 1; i <= length; i++) {
String sub = s.substring(0, i);
if (isPalindrome(sub)) {
generate(result, list, s.substring(i, length));
list.remove(list.size() - 1);
}
}
}

private boolean isPalindrome(String s) {
int len = s.length();
for (int i = 0; i < len / 2; i++) {
if (s.charAt(i) != s.charAt(len - 1 - i)) {
return false;
}
}
return true;
}
}
``````

Palindrome Partitioning II

````java````public class Solution {

public int minCut(String s) {
int length = s.length();
boolean[][] isPalindrome = new boolean[length][length];
int[] numOfCuts = new int[length + 1];
numOfCuts[0] = -1;
for (int i = 1; i < length + 1; i++) {
numOfCuts[i] = numOfCuts[i - 1] + 1;
}
for (int end = 0; end < length; end++) {
for (int begin = end; begin >= 0; begin--) {
if (s.charAt(begin) == s.charAt(end) && (end - begin < 2 || isPalindrome[begin + 1][end - 1])) {
isPalindrome[begin][end] = true;
numOfCuts[end + 1] = Math.min(numOfCuts[end + 1], numOfCuts[begin] + 1);
}
}
}
return numOfCuts[numOfCuts.length - 1];
}
}
``````

1.1k 声望
63 粉丝
0 条评论