# 简化路径

题目链接: <https://leetcode.cn/problems/simplify-path>

## 解题思路：

1. 按照/拆分路径字符串，分割成字符串数组
2. 遍历字符串数组，对于不同元素进行不同的操作
3. 如果字符串是`..`则进行出队操作，如果是`.`或空格则不动，其他元素均进行入队操作
4. 入队出队的顺序按照先进后出的逻辑进行
5. 如果队列长度为0，则不执行出队操作

```go
func simplifyPath(path string) string {
	pathList := strings.Split(path, "/")
	queue := make([]string, 0)
	for _,item:=range pathList{
        // ..表示进入上一层目录，移除掉队尾元素
		if item==".."{
			if len(queue)>0{
				queue=queue[:len(queue)-1]
			}
		}else if item!=""&& item!="."{
            // 既不是空格也不是.就入队
			queue=append(queue,item)
		}
	}
	return fmt.Sprintf("/%s",strings.Join(queue,"/"))
}
```

## 复杂度分析

* **时间复杂度：** 只遍历了一遍字符串，因此时间复杂度为 $$O(n)$$，其中 $$n$$ 是字符串 `s` 的长度
* **空间复杂度：** 空间复杂度为 $$O(n)$$，函数使用了一个长度为 $$n$$ 的切片来存储栈。在最坏的情况下，栈的深度可能达到 $$n$$，所以空间复杂度为 $$O(n)$$


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://leetcodebook-1.gitbook.io/top-interview-150/stack/simplify-path.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
