71.简化路径
来源:互联网 发布:工业设计需要软件 编辑:程序博客网 时间:2024/05/21 17:31
Simplify Path
问题描述:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = “/home/”, => “/home”
path = “/a/./b/../../c/”, => “/c”
测试代码(c++):
class Solution {public: string simplifyPath(string path) { vector<string> st; vector<string> nodes; string result; split(path, '/', nodes); for(auto node : nodes) { //empty or only '.' path no change if(node == "" || node == ".") continue; //go back to the upper directory if(node == ".." && !st.empty()) st.pop_back(); //push back the current directory else if(node != "..") st.push_back(node); } for(auto it : st) result += "/" + it; return result.empty() ? "/" : result; } void split(string s, char delim, vector<string>& nodes) { string temp; stringstream ss(s); while(getline(ss, temp, delim)) { nodes.push_back(temp); } }};
性能:
参考答案(python):
class Solution(object): def simplifyPath(self, path): places = [p for p in path.split("/") if p!="." and p!=""] stack = [] for p in places: if p == "..": if len(stack) > 0: stack.pop() else: stack.append(p) return "/" + "/".join(stack)
性能:
阅读全文
0 0
- 71.简化路径
- 简化路径
- 简化路径
- 简化路径
- LeetCode 71. Simplify Path(简化路径)
- LeetCode-----71. Simplify Path(简化文件路径)
- 71. Simplify Path (简化路径)
- 简化页面访问路径
- LintCode-简化路径
- lintcode-简化路径-421
- 文件路径简化-堆栈
- 简化路径-LintCode
- Leetcode 71. Simplify Path 简化路径 解题报告
- Yii 框架 URL路径简化
- 简化浏览器地址栏访问路径
- *[Lintcode]Simplify Path简化路径
- leetcode【第十二周】简化路径
- Unix路径简化(模拟题)
- 扩展kmp入门---扩展kmp
- [笔记分享] [Build] Android编译系统源代码之config.mk
- 移植QT5.6到嵌入式开发板(史上最详细的QT移植教程)
- 编程思维题目-对喝汽水问题的解法整理
- raspberryPi树莓派安装nodejs
- 71.简化路径
- Qt 播放音频文件
- Mac系统终端命令行不执行命令 总出现command not found解决方法
- 博客绑定域名
- PX4代码学习系列博客(4)——uORB相关
- Android Wifi Direct
- Android手动编写ButterKnife编译时注解框架
- 大数据及人工智能基础系列8 图解反向传播算法
- Java常用API----字符串操作