每天刷個演算法題20160524:阿克曼函式的遞迴轉非遞迴解法
版權所有。所有權利保留。
歡迎轉載,轉載時請註明出處:
http://blog.csdn.net/xiaofei_it/article/details/51524754
為了防止思維僵化,每天刷個演算法題。已經刷了幾天了,現在發點程式碼。
我已經建了一個開源專案,每天的題目都在裡面:
https://github.com/Xiaofei-it/Algorithms
絕大部分演算法都是我自己寫的,沒有參考網上通用程式碼。讀者可能會覺得有的程式碼晦澀難懂,因為那是我自己的理解。
最近幾天都是在寫一些原來的東西,大多數是非遞迴。以後準備刷點DP、貪心之類的題。
下面是阿克曼函式的遞迴轉非遞迴解法。
/**
*
* Copyright 2016 Xiaofei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package xiaofei.algorithm;
import java.util.Stack;
/**
* Created by Xiaofei on 16/5/23.
*
* 今天把明天的程式碼寫了吧。
*/
public class AckermannFunction {
public static long calculateRecursively(long m, long n) {
if (m == 0) {
return n + 1;
} else if (m > 0 && n == 0) {
return calculateRecursively(m - 1, 1);
} else if (m > 0 && n > 0) {
return calculateRecursively(m - 1, calculateRecursively(m, n - 1));
} else {
throw new IllegalArgumentException();
}
}
public static long calculateCorecursively(long m, long n) {
class Element {
long m;
long n;
long r;
Element(long m, long n) {
this.m = m;
this.n = n;
this.r = -1;
}
}
Stack<Element> stack = new Stack<>();
stack.push(new Element(m, n));
long tmp = -1;
while (!stack.isEmpty()) {
Element element = stack.peek();
if (element.m == 0) {
tmp = element.n + 1;
stack.pop();
} else if (element.m > 0 && element.n == 0) {
if (tmp >= 0) {
stack.pop();
} else {
stack.push(new Element(element.m - 1, 1));
}
} else {
if (element.r < 0) {
if (tmp >= 0) {
element.r = tmp;
tmp = -1;
stack.push(new Element(element.m - 1, element.r));
} else {
stack.push(new Element(element.m, element.n - 1));
}
} else {
if (tmp >= 0) {
stack.pop();
} else {
throw new IllegalStateException();
}
}
}
}
return tmp;
}
}
相關文章
- 揹包問題的遞迴與非遞迴演算法遞迴演算法
- Python 八皇后解法(非遞迴版本)Python遞迴
- 函式的遞迴函式遞迴
- 遞迴函式遞迴函式
- 快速排序【遞迴】【非遞迴】排序遞迴
- 快速排序(遞迴及非遞迴演算法原始碼)排序遞迴演算法原始碼
- 遞迴函式的理解遞迴函式
- 函式之遞迴函式遞迴
- 【C++】翻轉二叉樹(遞迴、非遞迴)C++二叉樹遞迴
- 函式表示式–遞迴函式遞迴
- 1.5.6 python遞迴函式Python遞迴函式
- 13.0、python遞迴函式Python遞迴函式
- 資料結構與演算法——歸併排序: 陣列&連結串列&遞迴&非遞迴解法全家桶資料結構演算法排序陣列遞迴
- 函式遞迴與生成式函式遞迴
- 遍歷二叉樹-------遞迴&非遞迴二叉樹遞迴
- Python 函式進階-遞迴函式Python函式遞迴
- 二叉樹——後序遍歷的遞迴與非遞迴演算法二叉樹遞迴演算法
- 五大演算法程式碼模板(DFS 遞迴非遞迴都算上,是六個)演算法遞迴
- 初學 PHP 函式的遞迴PHP函式遞迴
- day 17 – 1 遞迴函式遞迴函式
- 遞迴函式-樹形列表遞迴函式
- 遞迴函式例項大全遞迴函式
- 遞迴轉非遞迴 棧模擬 Recursive to Non-recursive stack simulated 總結遞迴
- python-動態規劃的遞迴、非遞迴實現Python動態規劃遞迴
- Vue3.0的遞迴監聽和非遞迴監聽Vue遞迴
- 二十一、氣泡排序演算法——JAVA實現(遞迴與非遞迴)排序演算法Java遞迴
- JS函式表示式——函式遞迴、閉包JS函式遞迴
- 第 8 節:函式-匿名函式、遞迴函式函式遞迴
- GO語言————6.6 遞迴函式Go遞迴函式
- [20180531]函式呼叫與遞迴.txt函式遞迴
- bilibiliclass10and11_函式遞迴函式遞迴
- C#語言函式遞迴C#函式遞迴
- 遞迴和非遞迴分別實現求n的階乘遞迴
- 二叉樹的四種遍歷(遞迴與非遞迴)二叉樹遞迴
- 演算法小專欄:遞迴與尾遞迴演算法遞迴
- 遞迴和尾遞迴遞迴
- 遞迴演算法遞迴演算法
- Android遍歷所有控制元件的遞迴和非遞迴實現Android控制元件遞迴
- 為什麼你學不會遞迴?刷題幾個月,告別遞迴,談談我的經驗遞迴