enhance I42A6V

This commit is contained in:
Looly
2021-07-26 17:41:39 +08:00
parent 6468a0446d
commit fd6885441e
3 changed files with 28 additions and 10 deletions

View File

@@ -95,16 +95,15 @@ public class TextSimilarity {
* @return 公共子串
*/
private static String longestCommonSubstring(String strA, String strB) {
char[] chars_strA = strA.toCharArray();
char[] chars_strB = strB.toCharArray();
int m = chars_strA.length;
int n = chars_strB.length;
int m = strA.length();
int n = strB.length();
// 初始化矩阵数据,matrix[0][0]的值为0 如果字符数组chars_strA和chars_strB的对应位相同则matrix[i][j]的值为左上角的值加1 否则matrix[i][j]的值等于左上方最近两个位置的较大值, 矩阵中其余各点的值为0.
int[][] matrix = new int[m + 1][n + 1];
// 初始化矩阵数据,matrix[0][0]的值为0 如果字符数组chars_strA和chars_strB的对应位相同则matrix[i][j]的值为左上角的值加1
// 否则matrix[i][j]的值等于左上方最近两个位置的较大值, 矩阵中其余各点的值为0.
final int[][] matrix = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (chars_strA[i - 1] == chars_strB[j - 1]) {
if (strA.charAt(i - 1) == strB.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1] + 1;
} else {
matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]);
@@ -112,7 +111,8 @@ public class TextSimilarity {
}
}
// 矩阵中如果matrix[m][n]的值不等于matrix[m-1][n]的值也不等于matrix[m][n-1]的值, 则matrix[m][n]对应的字符为相似字符元并将其存入result数组中。
// 矩阵中如果matrix[m][n]的值不等于matrix[m-1][n]的值也不等于matrix[m][n-1]的值,
// 则matrix[m][n]对应的字符为相似字符元并将其存入result数组中。
char[] result = new char[matrix[m][n]];
int currentIndex = result.length - 1;
while (matrix[m][n] != 0) {
@@ -121,7 +121,7 @@ public class TextSimilarity {
} else if (matrix[m][n] == matrix[m - 1][n]) {
m--;
} else {
result[currentIndex] = chars_strA[m - 1];
result[currentIndex] = strA.charAt(m - 1);
currentIndex--;
n--;
m--;

View File

@@ -2,6 +2,7 @@ package cn.hutool.core.util;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.math.Calculator;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -2657,6 +2658,21 @@ public class NumberUtil {
return false == (Float.isNaN(number) || Float.isInfinite(number));
}
/**
* 计算数学表达式的值,只支持加减乘除和取余<br>
* 如:
* <pre class="code">
* calculate("(0*1--3)-5/-4-(3*(-2.13))") -》 10.64
* </pre>
*
* @param expression 数学表达式
* @return 结果
* @since 5.7.6
*/
public static double calculate(String expression){
return Calculator.conversion(expression);
}
// ------------------------------------------------------------------------------------------- Private method start
private static int mathSubNode(int selectNum, int minNum) {
if (selectNum == minNum) {