每日一题(2022-5-24):经典搜索算法

Java69

题目

给定一个文件目录的路径,统计这个目录下所有的文件数并返回

分析

这个问题很容易想到深度搜索或广泛搜索。它不仅检查搜索算法的应用,而且检查文件的操作。这个问题在面试中很常见

[En]

This question is easy to think of deep search or extensive search. It not only examines the application of search algorithms, but also examines the operation of files. This question is very common in interviews

直接看代码吧,边看注释边讲。

代码

深度搜素:

```java
public static int dfs(String folderPath) {
File root = new File(folderPath);
// 如果传过来的参数不是目录,直接返回 0
if (!root.isDirectory()) {
return 0;
}

输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长