13-line 0ms c++ solution using string stream


  • 2
    V
    class Solution {
    public:
        int lengthLongestPath(string input) {
            vector<int> rec;
            int max_val = 0;
            stringstream ss(input);
            string cur;
            while(getline(ss, cur, '\n')){
                int i = 0;
                while(cur[i] == '\t')++i;
                if(i+1 > rec.size())
                    rec.push_back(0);
                rec[i] = (i ? rec[i-1] : 0) + cur.size()-i+1;
                if(cur.find('.') != string::npos)max_val = max(max_val, rec[i]-1);
            }
            return max_val;
        }
    };
    

  • 0
    F
    rec[i] = rec[i-1] + cur.size()-i+1;
    

    won't this have a problem if i==0?


  • 0
    V
    This post is deleted!

  • 0
    V

    @fiveface
    I think you are right. I wasn't being careful enough. Not sure why it was accepted though.
    Thanks for pointing that out. I've fixed it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.