재귀(Recursion)는 자신을 정의할 때 자기 자신을 재참조하는 방법이다
private static int factorial(int num) {
if (num > 1) {
if (arr[num] == 0)
arr[num] = num * factorial(num - 1);
else
return arr[num];
return arr[num];
}
else
arr[num] = num;
return num;
}
private static int list_sum(List<Integer> list){
if (list.size() == 1)
return list.get(0);
else if (list.size() > 1)
{
int last = list.get(list.indexOf(list.size() - 1));
list.remove(list.indexOf(list.size() - 1));
return last + list_sum(list);
}
else
return 0;
}
private static boolean check_circle_str(String str){
if (str.length() <= 1)
return true;
if (str.charAt(0) == str.charAt(str.length()- 1) )
return check_circle_str(str.substring(1, str.length() - 1));
else
return false;
}
private static int by123(int n)
{
// f(n) = f(n - 1) + f(n -2) + f(n - 3)
if (n == 1)
return 1;
if (n == 2)
return 2;
if (n == 3)
return 4;
else if (n >= 4)
return by123(n-3) + by123(n-2) + by123(n-1);
else
return 0;
}