我在学校理解递归方面遇到了很大麻烦.每当教授谈论它时,我似乎都能得到它,但是只要我自己尝试它就会彻底打动我的大脑.
我整晚都试图解决河内塔楼,并彻底打动了我的思绪.我的教科书在递归时只有大约30页,所以它不太有用.有谁知道可以帮助澄清这个主题的书籍或资源?
任何人都可以用PHP(不使用Fibonacci)在外行语言和使用示例中向我解释一个递归函数吗?我正在看一个例子,但斐波那契完全失去了我!
提前谢谢;-)您还经常在Web开发中使用它们吗?
我试图更好地理解递归和缓存,但我仍然取得了有趣的进展(有时候我有点慢).我在理解这段代码时遇到小问题:
# Fibonacci recursive with result storage
class FibonacciStorage:
_storage = { 0:1, 1:1 }
@staticmethod
def _fib(number):
try: # is this already calculated
return FibonacciStorage._storage[number] #searches dict, and if value exists then return it
except KeyError:
result = FibonacciStorage._fib(number-1) + FibonacciStorage._fib(number-2) #this is the actual Fibonacci Formula
FibonacciStorage._storage[number] = result #adds result to dictionary
#print FibonacciStorage._storage #this shows the storage list growing with each iteration.
return result
@staticmethod
def fib(number): #first function, it has two asserts to basically make …Run Code Online (Sandbox Code Playgroud) public class Employee {
int id;
String value;
@Override
public boolean equals(Object obj) {
return true; }
@Override
public int hashCode() {
System.out.println("hi this is my hashcode " + this.hashCode());
return this.hashCode();}
}
public class TestCH {
public static void main(String args[])
{
Employee emp1= new Employee();
Employee emp2= new Employee();
Employee emp3= new Employee();
Employee emp4= new Employee();
Map map= new HashMap();
map.put( emp1,1);
map.put(emp2,2);
map.put(emp3,3);
System.out.println("with map "+map.size());
}
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中我试图通过System.out.println打印hashcode生成StackOverflowError.为什么我得到StackOverflowError谢谢