开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (2)
标签 (2)
main
dev
1.1
1.0
main
分支 (2)
标签 (2)
main
dev
1.1
1.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (2)
标签 (2)
main
dev
1.1
1.0
algorithms
/
recursion
/
basic
/
RecursionPy.java
algorithms
/
recursion
/
basic
/
RecursionPy.java
RecursionPy.java 4.60 KB
一键复制 编辑 原始数据 按行查看 历史
Jarry 提交于 2026年04月06日 09:25 +08:00 . update comments for recursion
import java.util.HashMap;
import java.util.Map;
/**
* 递归算法示例集合(基于Python版本)
* 包含阶乘、斐波那契、汉诺塔、二分查找、数组求和、字符串反转等示例
*/
public class RecursionPy {
// 1. 阶乘 (Factorial)
// 时间复杂度: O(n), 空间复杂度: O(n)(递归栈深度)
public static int factorial(int n) {
/* 计算 n 的阶乘 */
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
// 2. 斐波那契数列
// 普通递归: 时间复杂度 O(2^n),空间复杂度 O(n)
public static int fibonacci(int n) {
/* 计算斐波那契数列第 n 项(普通递归) */
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
// 优化版本(记忆化)
public static int fibonacciMemo(int n, Map<Integer, Integer> memo) {
/* 计算斐波那契数列第 n 项(带记忆化) */
if (memo.containsKey(n)) {
return memo.get(n);
}
if (n <= 1) {
memo.put(n, n);
} else {
memo.put(n, fibonacciMemo(n - 1, memo) + fibonacciMemo(n - 2, memo));
}
return memo.get(n);
}
public static int fibonacciMemo(int n) {
return fibonacciMemo(n, new HashMap<>());
}
// 3. 汉诺塔问题
// 时间复杂度: O(2^n - 1),空间复杂度: O(n)
public static void hanoi(int n, String source, String target, String auxiliary) {
/*
* 解决汉诺塔问题
* 将 n 个盘子从 source 柱移动到 target 柱
*/
if (n == 1) {
System.out.println("Move disk 1 from " + source + " to " + target);
return;
}
// 将 n-1 个盘子从 source 移动到 auxiliary
hanoi(n - 1, source, auxiliary, target);
// 将最后一个盘子从 source 移动到 target
System.out.println("Move disk " + n + " from " + source + " to " + target);
// 将 n-1 个盘子从 auxiliary 移动到 target
hanoi(n - 1, auxiliary, target, source);
}
// 4. 二分查找(递归版本)
// 时间复杂度: O(log n),空间复杂度: O(log n)
public static int binarySearch(int[] arr, int target, int low, int high) {
/* 在排序数组中递归查找目标值 */
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
return binarySearch(arr, target, low, mid - 1);
} else {
return binarySearch(arr, target, mid + 1, high);
}
}
// 5. 数组求和
// 时间复杂度: O(n),空间复杂度: O(n)
public static int arraySum(int[] arr, int index) {
/* 递归计算数组元素和 */
if (index >= arr.length) {
return 0;
}
return arr[index] + arraySum(arr, index + 1);
}
public static int arraySum(int[] arr) {
return arraySum(arr, 0);
}
// 6. 反转字符串
// 时间复杂度: O(n),空间复杂度: O(n)
public static String reverseString(String s) {
/* 递归反转字符串 */
if (s.isEmpty()) {
return "";
}
return reverseString(s.substring(1)) + s.charAt(0);
}
public static void main(String[] args) {
System.out.println("=== 递归算法测试 ===\n");
// 测试阶乘
System.out.println("1. 阶乘");
System.out.println("5! = " + factorial(5));
// 测试斐波那契
System.out.println("\n2. 斐波那契数列");
System.out.println("fib(10) = " + fibonacci(10));
System.out.println("fib(10) with memo = " + fibonacciMemo(10));
// 测试汉诺塔
System.out.println("\n3. 汉诺塔问题 (n=3)");
hanoi(3, "A", "C", "B");
// 测试二分查找
System.out.println("\n4. 二分查找");
int[] arr = {1, 3, 5, 7, 9, 11, 13};
System.out.println("查找 7: 索引 = " + binarySearch(arr, 7, 0, arr.length - 1));
// 测试数组求和
System.out.println("\n5. 数组求和");
int[] sumArr = {1, 2, 3, 4, 5};
System.out.println("sum([1,2,3,4,5]) = " + arraySum(sumArr));
// 测试字符串反转
System.out.println("\n6. 字符串反转");
System.out.println("reverse('hello') = " + reverseString("hello"));
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pyxstudy/algorithms.git
git@gitee.com:pyxstudy/algorithms.git
pyxstudy
algorithms
algorithms
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /