遍历文件树
public static void main(String[] args) {
FileList.loadFile("D:\\mq\\3.0",0);
}
/**
* 递归遍历
* @param path 文件路径
* @param num 文件深度
*/
public static void loadFile(String path,int num) {
if (null == path || "".equals(path)){
throw new RuntimeException("文件路径错误!");
}
for (int i = 0; i < num; i++) {
System.out.print("\t");
}
File file = new File(path);
System.out.println(file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
// 进入下一级目录深度+1
loadFile(f.getPath(),num+1);
}
}
}
阶乘
public static int factorial(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
return n * factorial(n - 1);
}
爬楼梯
static Map<Integer, Long> map = new HashMap<>(16);
public static Long staircase(int n) {
if (n == 1) {
return 1L;
}
if (n == 2) {
return 2L;
}
if (null != map.get(n)) {
return map.get(n);
} else {
Long result = staircase(n - 1) + staircase(n - 2);
map.put(n, result);
return result;
}
}