如何将递归转换为循环

动机

  1. 递归效率没有循环高,有额外的方法调用开销
  2. 堆栈溢出(stackoverflow)
  3. 递归有时挺难理解(不过很多算法用递归最容易实现)

直接法

  1. 首先找到递归的结束条件,并且每次递归调用肯定是逼近结束条件(Base Case)
  2. 实现一个相同结束条件的循环,每次循环逼近结束条件
public class CountDown {
	public void countDown(int n) {
		if(n == 0) return;

		System.out.println(n + "...");
		waitASecond();
		countDown(n-1);
	}

	public void waitASecond() {
		try {
			Thread.sleep(1000);
		}
		catch (InterruptedException ignore) {
		}
	}

	public static void main(String[] args) {
		CountDown c = new CountDown();
		c.countDown(10);
	}
}

重构后

public class CountDown {
	public void countDown(int n) {
		while(n > 0) {
			System.out.println(n + "...");
			waitASecond ();
			n -= 1;
		}

	}

	public void waitASecond() {
		try {
			Thread.sleep(1000);
		}
		catch (InterruptedException ignore) {
		}
	}

	public static void main(String[] args) {
		CountDown c = new CountDown();
		c.countDown(10);
	}

显式栈法

通过显示定义的栈模拟系统方法栈,将每个方法的局部变量存入自定义栈中,然后再循环

package com.lab0.test2;

public class TestFactorial1 {
	public static int factorial(int n) {
		if (n == 0)
			return 1;
		else
			return n * factorial(n - 1);
	}

	public static void main(String[] args) {
		System.out.println(factorial(3));
	}
}

转换后

package com.lab0.test2;

import com.lab1.test1.LinkedStack;

public class TestFactorial2 {
	public static void main(String[] args) {
		LinkedStack<Integer> stack = new LinkedStack<>();
		for (int i = 3; i > 0; i--) {
			stack.push(i);
		}
		int result = 1;
		for (int v : stack) {
			result *= v;
		}
		System.out.println(result);
	}
}

Happy learning !!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值