Golang 10 lines, '-1' base-case trick


  • 0
    A
    func lengthLongestPath(input string) int {
        lines, curLength, maxLength := strings.Split(input, "\n"), []int{-1}, 0
        for _, l := range lines {
            lDepth := strings.Count(l, "\t")
            curLength = append(curLength[0:lDepth+1], curLength[lDepth] - lDepth + len(l) + 1)
            if strings.Contains(l, ".") && curLength[lDepth+1] > maxLength {
                maxLength = curLength[lDepth+1]
            }
        }
        return maxLength
    }
    

Log in to reply
 

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