gpt4 book ai didi

java - 我可以从递归返回到 main 而不展开堆栈吗?

转载 作者:太空宇宙 更新时间:2023-11-04 13:48:24 25 4
gpt4 key购买 nike

我编写了一个递归方法来减少字符串的长度。如果字符串的长度小于 20,我想尝试扫描仪输入中的下一个字符串。有没有比我编码的方式更有效的方法?

public static void main(String [] args){  
try {
input = new Scanner(new FileInputStream("VectorKnots.txt"));
output = new PrintWriter(new FileOutputStream("ReduceKnots.txt"));
}catch (FileNotFoundException e){
System.out.println("there is a problem with the file");
System.exit(0);
}
while (input.hasNext()){
loop2 = input.nextLine();
loop1.delete(0, loop1.length());
loop1.append(loop2);
Triangle(loop1);
}
}
public static void Triangle(StringBuilder loop1){
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("27")){
trIndex = loop1.indexOf("27");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 4);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("72")){
trIndex = loop1.indexOf("72");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 4);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("63")){
trIndex = loop1.indexOf("63");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 5);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("63")){
trIndex = loop1.indexOf("63");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 5);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("46")){
trIndex = loop1.indexOf("46");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 2);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("64")){
trIndex = loop1.indexOf("64");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 2);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("57")){
trIndex = loop1.indexOf("57");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 3);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("75")){
trIndex = loop1.indexOf("75");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 3);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("52")){
trIndex = loop1.indexOf("52");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 6);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("25")){
trIndex = loop1.indexOf("25");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 6);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("34")){
trIndex = loop1.indexOf("34");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 7);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("43")){
trIndex = loop1.indexOf("43");
loop1.delete(trIndex, trIndex+1);
loop1.insert(trIndex, 7);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("1") && loop1.indexOf("1") + 2) == 0){
trIndex = loop1.indexOf("1");
loop1.delete(trIndex);
loop1.delete(trIndex + 1);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.toString().contains("0") && loop1.indexOf("1") + 2) == 1){
trIndex = loop1.indexOf("0");
loop1.delete(trIndex);
loop1.delete(trIndex + 1);
Triangle(loop1);
}
if (loop1.length() < 20){
return;
}
if (loop1.length() > 19){
out.println(loop1);
}

}
}

最佳答案

如果您使用迭代,则无需从深度嵌套递归返回。尝试:

public static void triangle(StringBuilder loop1) {
for (;;) {
if (loop1.length() < 20)
return;
int trIndex = loop1.indexOf("27");
if ( trIndex >= 0 ) {
loop1.delete(trIndex+1);
loop1.setCharAt(trIndex,'2');
continue; // start over using loop instead of recursion
}

trIndex = loop1.indexOf("72");
if ( trIndex >= 0 ) {
loop1.delete(trIndex+1);
loop1.setCharAt(trIndex,'7');
continue; // start over using loop instead of recursion
}

// more cases

break;
}
}

另请注意

  1. 最后的测试:if ( ... > 19 ) out.println(loop1)不会做任何事情,因为你有 if ( ... < 20 ) return;就在之前;可能想将打印行移至 main ()
  2. contains () 只是执行 indexOf () >= 0,因此您最好单独执行此操作,而不是同时执行这两个操作。
  3. StringBuilder.insert(loc,int) 将 int 转换为字符串,然后运行 ​​StringBuilder.insert 的字符串版本;那么为什么不直接给它喂一根绳子呢。
  4. 您可能会考虑构建某种自动机,也许只查看字符串中的每个字符一次。

关于java - 我可以从递归返回到 main 而不展开堆栈吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30580485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com