• longest-absolute-file-path


    https://leetcode.com/problems/longest-absolute-file-path/
    
    public class Solution {
        public int lengthLongestPath(String input) {
            Stack<Integer> stk = new Stack<Integer>();
            int left = 0;
            int curStrlen = 0;
            int curTab = 0;
            int ret = 0;
            boolean hasDot = false;
            boolean isFile = false;
            
            for (int i=0; i<input.length(); i++) {
                // 
    
                if (input.charAt(i) == 10) {
    
                    curStrlen = i-left;
                    if (!stk.empty()) {
                        curStrlen += 1 + stk.peek();
                    }
                        
                    if (isFile && curStrlen > ret) {
                        ret  = curStrlen;
                    }
                    stk.push(curStrlen);
                    hasDot = false;
                    isFile = false;
                    curTab = 0;
                    left = -1;
                }
                // 	
                else if (input.charAt(i) == 9 && curTab < stk.size()) {
                    curTab++;
                }
                else if (input.charAt(i) == ' ' && i+3 < input.length() &&
                         input.substring(i, i+4).equals("    ") && curTab < stk.size()) {
                    curTab++;
                    i += 3;
                }
                else {
                    if (left == -1) {
                        int stkLen = stk.size();
                        for (int j=0; j<stkLen-curTab; j++) {
                            stk.pop();
                        }
                        left = i;
                    }
                    if (input.charAt(i) == '.') {
                        hasDot = true;
                    }
                    else {
                        if (hasDot) {
                            isFile = true;
                        }
                    }
                }
    
            }
            
            curStrlen = input.length()-left;
            if (!stk.empty()) {
                curStrlen += 1 + stk.peek();
            }
            if (isFile && curStrlen > ret) {
                ret = curStrlen;
            }
            return ret;
        }
    }
  • 相关阅读:
    如何优化电量
    140816 记录
    git 提交远程
    HttpClient post json
    JAVA static 作用
    Android Configuration change属性
    Android横竖屏切换处理
    android 多语言版本开发
    JAVA线程池简介
    30个你 “ 不可能全部会做 ” 的javascript题目-答案解释
  • 原文地址:https://www.cnblogs.com/charlesblc/p/5820344.html
Copyright © 2020-2023  润新知