leetcode388

该博客探讨了一种解决如何记录已遍历节点的方法,主要针对字符串中表示目录结构的问题。提供了两种策略:递归和使用栈。代码示例展示了如何通过计算每个节点的深度和长度来找到最长的路径。在处理过程中,利用栈来跟踪目录深度,并通过比较深度来确定当前节点的位置。此外,还提到了使用数组作为替代方案。
摘要由CSDN通过智能技术生成

题目
考查如何保存上一次的结果方法一:递归,方法二:数组,或者栈

//我刚开始的思想是直接对其进行'\t'分割,递归之后对于'\t\t'分割,
    // 但是发现,第一次递归对于'\t'分割,已经不能行的通了。
    //既然递归行不通,可以借助栈,采取手动解析的方式。
    //如何判断下一个节点是栈顶元素的下一个目录节点?采用深度来判断。每个'\t'代表一个深度。

    //其实这道题就是考查,如何记录已经遍历过的节点,方法一:递归,方法二:数组,或者栈
    public int lengthLongestPath(String input) {
        int length = input.length();
        int pos = 0;
        int ans = 0;
        Deque<Integer> stack = new LinkedList<>();
        //根目录拥有多个目录情况下。
        while (pos < length) {
            int depth = 1;
            //计算当前节点深度
            while (pos < length && input.charAt(pos) == '\t') {
                pos++;
                depth++;
            }
            //计算当前节点长度
            int len = 0;
            //计算是否为文件
            boolean isFile = false;
            while (pos < length && input.charAt(pos) != '\n') {
                if (input.charAt(pos) == '.') {
                    isFile = true;
                }
                pos++;
                len++;
            }
            // 跳过当前的换行符
            pos++;
            while (stack.size() >= depth) {
                stack.pop();
            }
            if (!stack.isEmpty()) {
                // + 1就是绝对位置的/需要加进去
                len += stack.peek() + 1;
            }
            if (isFile) {
                ans = Math.max(ans, len);
            } else {
                stack.push(len);
            }

        }
        return ans;
    }
    //同样,我们可以使用数组代替栈
    class Solution {
        public int lengthLongestPath(String input) {
            int n = input.length();
            int pos = 0;
            int ans = 0;
            int[] level = new int[n + 1];

            while (pos < n) {
                /* 检测当前文件的深度 */
                int depth = 1;
                while (pos < n && input.charAt(pos) == '\t') {
                    pos++;
                    depth++;
                }
                /* 统计当前文件名的长度 */
                int len = 0;
                boolean isFile = false;
                while (pos < n && input.charAt(pos) != '\n') {
                    if (input.charAt(pos) == '.') {
                        isFile = true;
                    }
                    len++;
                    pos++;
                }
                /* 跳过换行符 */
                pos++;

                if (depth > 1) {
                    len += level[depth - 1] + 1;
                }
                if (isFile) {
                    ans = Math.max(ans, len);
                } else {
                    level[depth] = len;
                }
            }
            return ans;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值