add
This commit is contained in:
parent
73748b9dd7
commit
42bbdd309b
66
5.leetcode/src/com/fanxb/common/Q131.java
Normal file
66
5.leetcode/src/com/fanxb/common/Q131.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 分割回文串
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/9 10:10
|
||||
*/
|
||||
public class Q131 {
|
||||
|
||||
public List<List<String>> partition(String s) {
|
||||
//先用dp找到所有符合条件的回文
|
||||
boolean[][] dp = new boolean[s.length()][s.length()];
|
||||
for (int j = 0; j < dp.length; j++) {
|
||||
for (int i = 0; i <= j; i++) {
|
||||
if (i == j) {
|
||||
dp[i][j] = true;
|
||||
} else {
|
||||
boolean b = s.charAt(i) == s.charAt(j);
|
||||
if (i + 1 == j) {
|
||||
dp[i][j] = b;
|
||||
} else {
|
||||
dp[i][j] = dp[i + 1][j - 1] && b;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
List<List<String>> res = new ArrayList<>();
|
||||
dfs(s, 0, new Stack<>(), res, dp);
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 分割字符串
|
||||
*
|
||||
* @param s 字符串
|
||||
* @param index 子串的起始位置
|
||||
* @param temp 临时存储已经分割后的字符串
|
||||
* @param res 存储最终分割结果
|
||||
* @param dp dp结果
|
||||
* @author fanxb
|
||||
* date 2022/3/9 16:39
|
||||
*/
|
||||
private void dfs(String s, int index, Stack<String> temp, List<List<String>> res, boolean[][] dp) {
|
||||
if (index == s.length()) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
return;
|
||||
}
|
||||
for (int i = index; i < s.length(); i++) {
|
||||
if (dp[index][i + 1]) {
|
||||
String tempS = s.substring(index, i + 1);
|
||||
temp.push(tempS);
|
||||
dfs(s, i + 1, temp, res, dp);
|
||||
temp.pop();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
51
5.leetcode/src/com/fanxb/common/Q132.java
Normal file
51
5.leetcode/src/com/fanxb/common/Q132.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* 分割回文串
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/9 10:10
|
||||
*/
|
||||
public class Q132 {
|
||||
|
||||
public int minCut(String s) {
|
||||
//先用dp找到所有符合条件的回文
|
||||
boolean[][] dp = new boolean[s.length()][s.length()];
|
||||
for (int j = 0; j < dp.length; j++) {
|
||||
for (int i = 0; i <= j; i++) {
|
||||
if (i == j) {
|
||||
dp[i][j] = true;
|
||||
} else {
|
||||
boolean b = s.charAt(i) == s.charAt(j);
|
||||
if (i + 1 == j) {
|
||||
dp[i][j] = b;
|
||||
} else {
|
||||
dp[i][j] = dp[i + 1][j - 1] && b;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//再次dp找到最小分割次数.定义minArr[i]为从0到i的最小分割次数
|
||||
int[] minArr = new int[s.length()];
|
||||
for (int i = 1; i < s.length(); i++) {
|
||||
//定义l,当l->i构成回文串时,此时分割次数为=minArr[l-1]+1.找到最小的l
|
||||
int temp = i;
|
||||
for (int l = 0; l <= i; l++) {
|
||||
if (dp[l][i]) {
|
||||
temp = Math.min(temp, l == 0 ? 0 : minArr[l - 1] + 1);
|
||||
}
|
||||
}
|
||||
minArr[i] = temp;
|
||||
}
|
||||
return minArr[s.length() - 1];
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q132().minCut("aab"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user