388. Longest Absolute File Path

来源:互联网 发布:jquery json 查找 编辑:程序博客网 时间:2024/05/17 07:58

提交了三次才AC,==为啥这种题目老是各种错误,都没有想到。

  1. 感觉只要在由i++的地方,都要判断一下是不是会超范围。
  2. 只要有更新sum的地方都要更新最大值。
class Solution {public:    int lengthLongestPath(string input) {        int maxSum=0;        int sum=0;        stack<int> nums;        stack<int> depth;        int i=0;        string temp;        while(i<input.size()&&input[i]!='\n')            temp+=input[i++];        nums.push(temp.length());        sum+=temp.length();        depth.push(0);        if(temp.find('.')!=std::string::npos)            maxSum=max(maxSum,sum);        while(i<input.size())        {            int depthTemp=0;            while(input[++i]=='\t')                depthTemp++;            string temp;            while(i<input.size()&&input[i]!='\n')                temp+=input[i++];            //cout<<temp<<endl;            while((!depth.empty())&&depthTemp<=depth.top())            {                sum-=nums.top();                nums.pop();                depth.pop();            }            depth.push(depthTemp);            nums.push(temp.length()+(depthTemp!=0));            sum+=nums.top();            if(temp.find('.')!=std::string::npos)                maxSum=max(maxSum,sum);        }        return maxSum;    }};
0 0
原创粉丝点击