Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

click to show corner cases.

Corner Cases:

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".

这道题让简化给定的路径,光根据题目中给的那一个例子还真不太好总结出规律,应该再加上两个例子 path = "/a/./b/../c/", => "/a/c"和path = "/a/./b/c/", => "/a/b/c", 这样我们就可以知道中间是"."的情况直接去掉,是".."时删掉它上面挨着的一个路径,而下面的边界条件给的一些情况中可以得知,如果是空的话返回"/",如果有多个"/"只保留一个。那么我们可以把路径看做是由一个或多个"/"分割开的众多子字符串,把它们分别提取出来一一处理即可,代码如下:

C++ 解法一:

class Solution {
public:
string simplifyPath(string path) {
vector<string> v;
int i = ;
while (i < path.size()) {
while (path[i] == '/' && i < path.size()) ++i;
if (i == path.size()) break;
int start = i;
while (path[i] != '/' && i < path.size()) ++i;
int end = i - ;
string s = path.substr(start, end - start + );
if (s == "..") {
if (!v.empty()) v.pop_back();
} else if (s != ".") {
v.push_back(s);
}
}
if (v.empty()) return "/";
string res;
for (int i = ; i < v.size(); ++i) {
res += '/' + v[i];
}
return res;
}
};

还有一种解法是利用了C语言中的函数strtok来分隔字符串,但是需要把string和char*类型相互转换,转换方法请猛戳这里。除了这块不同,其余的思想和上面那种解法相同,代码如下:

C 解法一:

class Solution {
public:
string simplifyPath(string path) {
vector<string> v;
char *cstr = new char[path.length() + ];
strcpy(cstr, path.c_str());
char *pch = strtok(cstr, "/");
while (pch != NULL) {
string p = string(pch);
if (p == "..") {
if (!v.empty()) v.pop_back();
} else if (p != ".") {
v.push_back(p);
}
pch = strtok(NULL, "/");
}
if (v.empty()) return "/";
string res;
for (int i = ; i < v.size(); ++i) {
res += '/' + v[i];
}
return res;
}
};

C++中也有专门处理字符串的机制,我们可以使用stringstream来分隔字符串,然后对每一段分别处理,思路和上面的方法相似,参见代码如下:

C++ 解法二:

class Solution {
public:
string simplifyPath(string path) {
string res, t;
stringstream ss(path);
vector<string> v;
while (getline(ss, t, '/')) {
if (t == "" || t == ".") continue;
if (t == ".." && !v.empty()) v.pop_back();
else if (t != "..") v.push_back(t);
}
for (string s : v) res += "/" + s;
return res.empty() ? "/" : res;
}
};

Java 解法二:

public class Solution {
public String simplifyPath(String path) {
Stack<String> s = new Stack<>();
String[] p = path.split("/");
for (String t : p) {
if (!s.isEmpty() && t.equals("..")) {
s.pop();
} else if (!t.equals(".") && !t.equals("") && !t.equals("..")) {
s.push(t);
}
}
List<String> list = new ArrayList(s);
return "/" + String.join("/", list);
}
}

参考资料:

https://discuss.leetcode.com/topic/8678/c-10-lines-solution

https://discuss.leetcode.com/topic/7675/java-10-lines-solution-with-stack

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Simplify Path 简化路径的更多相关文章

  1. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  2. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  3. [LeetCode] 71. Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  4. 【LeetCode每天一题】Simplify Path(简化路径)

    Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...

  5. 071 Simplify Path 简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...

  6. Leetcode71. Simplify Path简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...

  7. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. [LeetCode] Simplify Path,文件路径简化,用栈来做

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  9. Simplify Path(路径简化)

    问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...

随机推荐

  1. yum和apt-get有什么区别

    一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...

  2. 安装完成后在命令行运行bash时报错0x80070057

    在命令运行bash 提示如下: 解决方法,不启用旧版本控制台: 右键命令提示栏 打开属性,把勾选去掉如下图红色边框标识: 然后重启,就可以使用,也包括可以打开Bash on Unbuntu on Wi ...

  3. [Ant]Ant简易教程

    前言 Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.由Apache软件基金会所提供. Ant是纯Java语言编写的,所以具有 ...

  4. 解析ListView联动的实现--仿饿了么点餐界面

    一.博客的由来 大神王丰蛋哥 之前一篇博客仿饿了点餐界面2个ListView联动(http://www.cnblogs.com/wangfengdange/p/5886064.html) 主要实现了2 ...

  5. ABP集合贴

    thead>tr>th,.table>tbody>tr>th,.table>tfoot>tr>th,.table>thead>tr>t ...

  6. TYPESDK手游聚合SDK客户端远程开关:渠道支付黑名单

    渠道支付要做开关干嘛用呢?为什么要做这种东西呢? 这个教训来分享一下,我们的游戏上线公测了,59个渠道首发,其中包括了应用宝,UC,360等的大渠道,也包含有一些工会渠道和小渠道,上线后一切正常,但是 ...

  7. HTML学习(零)简介

    一)HTML介绍 它是一个超文本标记语言,静态页面. 所谓的'超文本'就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 主要的结构为包括"头"部分(英语:Head).和 ...

  8. 使用WebRTC搭建前端视频聊天室——点对点通信篇

    WebRTC给我们带来了浏览器中的视频.音频聊天体验.但个人认为,它最实用的特性莫过于DataChannel——在浏览器之间建立一个点对点的数据通道.在DataChannel之前,浏览器到浏览器的数据 ...

  9. Android 7.1 App Shortcuts使用

    Android 7.1 App Shortcuts使用 Android 7.1已经发了预览版, 这里是API Overview: API overview. 其中App Shortcuts是新提供的一 ...

  10. 浅谈Android编码规范及命名规范

    前言: 目前工作负责两个医疗APP项目的开发,同时使用LeanCloud进行云端配合开发,完全单挑. 现大框架已经完成,正在进行细节模块上的开发 抽空总结一下Android项目的开发规范:1.编码规范 ...