[英]java - how to avoid global external variable as output in recursive function
I have this code to find "All Longest Common Sequences" and their length.我有这个代码来查找“所有最长的公共序列”及其长度。
public class LCS {
static Set<String> lcsList = new HashSet<>();
public static int[][] twoStringMatrix(String a, String b) {
int arr[][] = new int[a.length() + 1][b.length() + 1];
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j < arr[0].length; j++) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
arr[i][j] = arr[i - 1][j - 1] + 1;
} else {
arr[i][j] = Math.max(arr[i][j - 1], arr[i - 1][j]);
}
}
}
return arr;
}
public static int lengthLcs(int[][] twoStringMatrix) {
return twoStringMatrix[twoStringMatrix.length - 1][twoStringMatrix[0].length - 1];
}
public static void allPaths(String a, String b, int i, int j, String s, int arr[][]) {
if (i > 0 && j > 0) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
allPaths(a, b, i - 1, j - 1, a.charAt(i - 1) + s, arr);
} else if (arr[i][j - 1] == arr[i - 1][j]) {
allPaths(a, b, i, j - 1, s, arr);
allPaths(a, b, i - 1, j, s, arr);
} else if (arr[i][j - 1] > arr[i - 1][j]) {
allPaths(a, b, i, j - 1, s, arr);
} else if (arr[i][j - 1] < arr[i - 1][j]) {
allPaths(a, b, i - 1, j, s, arr);
}
} else {
lcsList.add(s);
}
}
public static void main(String[] args) {
String b = "abbaecde";
String a = "abacbae";
System.out.println("length = " + twoStringMatrix(a, b).length);
System.out.println("length = " + twoStringMatrix(a, b)[0].length);
allPaths(a, b, a.length(), b.length(), "", twoStringMatrix(a, b));
System.out.println((lcsList));
}
}
The problem with this code is that I have to use lcsList
as a "global" variable.这段代码的问题是我必须使用lcsList
作为“全局”变量。
How can I avoid accessing outside variables in allPaths
?如何避免访问allPaths
外部变量?
I can probably do something like this, but it does not look right:我可能可以做这样的事情,但它看起来不对:
public static void getAll(String a, String b, int i, int j, String s, int arr[][]){
allPaths(a, b, a.length(), b.length(), "", twoStringMatrix(a, b));
System.out.println(lcsList);
lcsList.clear();
}
What if i have 100 functions in this class and they all have this outside variable?如果我在这个类中有 100 个函数并且它们都有这个外部变量怎么办? It seems like a bad practice.这似乎是一种不好的做法。
You can just pass a mutable container as an accumulator parameter like this: note the new paths
parameter.您可以像这样将可变容器作为累加器参数传递:注意新的paths
参数。
public static void allPaths(String a, String b, int i, int j, String s, int[][] arr, List<String> paths) {
if (i > 0 && j > 0) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
allPaths(a, b, i - 1, j - 1, a.charAt(i - 1) + s, arr, paths);
} else if (arr[i][j - 1] == arr[i - 1][j]) {
allPaths(a, b, i, j - 1, s, arr, paths);
allPaths(a, b, i - 1, j, s, arr, paths);
} else if (arr[i][j - 1] > arr[i - 1][j]) {
allPaths(a, b, i, j - 1, s, arr, paths);
} else if (arr[i][j - 1] < arr[i - 1][j]) {
allPaths(a, b, i - 1, j, s, arr, paths);
}
} else {
paths.add(s);
}
}
public static void main(String[] args) {
String b = "abbaecde";
String a = "abacbae";
final List<String> paths = new ArrayList<>();
allPaths(a, b, a.length(), b.length(), "", twoStringMatrix(a, b), paths);
System.out.println((paths));
}
The results I get contain duplicates ( [abbae, abace, abace, abace]
) so you might want to use Set
instead:我得到的结果包含重复项( [abbae, abace, abace, abace]
)所以你可能想使用Set
代替:
final Set<String> paths = new HashSet<>();
PS I'd also note that using string concatenation for constructing a path is not very effective, because a new String
object is created every time (as String
s are immutable). PS 我还注意到,使用字符串连接来构造路径并不是很有效,因为每次都会创建一个新的String
对象(因为String
是不可变的)。 You should rather use StringBuilder
and its insert
operation.您应该使用StringBuilder
及其insert
操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.