题目

简化路径

给定一个文档(Unix-style)的完全路径,请进行路径简化。

样例

"/home/", => "/home"

"/a/./b/../../c/", => "/c"

挑战

  • 你是否考虑了 路径 = "/../" 的情况?

    在这种情况下,你需返回"/"

  • 此外,路径中也可能包含双斜杠'/',如 "/home//foo/"

    在这种情况下,可忽略多余的斜杠,返回 "/home/foo"

解题

linux 没碰过,真的表示不知道这个是什么鬼

参考链接, 参考链接2

Unix的path规则可以在这里了解:
http://en.wikipedia.org/wiki/Path_(computing)

路径简化的依据是:

当遇到“/../"则需要返回上级目录,需检查上级目录是否为空。

当遇到"/./"则表示是本级目录,无需做任何特殊操作。

当遇到"//"则表示是本级目录,无需做任何操作。

当遇到其他字符则表示是文件夹名,无需简化。

当字符串是空或者遇到”/../”,则需要返回一个"/"。

当遇见"/a//b",则需要简化为"/a/b"。

先将字符串依"/"分割出来,然后检查每个分割出来的字符串。

当字符串为空或者为".",不做任何操作。

当字符串不为"..",则将字符串入栈。

当字符串为"..", 则弹栈(返回上级目录)。

这样栈内的字符就是答案,但是需要进行重新组合

以“/”隔开组合

public class Solution {
/**
* @param path the original path
* @return the simplified path
*/
public String simplifyPath(String path) {
// Write your code here
if(path==null)
return null;
String[] p = path.split("/");
Stack<String> stack = new Stack<String>();
for(int i=0;i<p.length;i++){
if(p[i].equals(".") || p[i].isEmpty()){
continue;
}else if(p[i].equals("..")){
if(!stack.empty()){
stack.pop();
}
}else{
stack.push(p[i]);
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.insert(0, stack.pop());
sb.insert(0, "/");
}
return sb.length() == 0 ? "/" : sb.toString(); }
}

Java Code

总耗时: 9414 ms

class Solution:
# @param {string} path the original path
# @return {string} the simplified path
def simplifyPath(self, path):
# Write your code here
if path == None:
return None
p = path.split("/")
stack = []
for s in p:
if s == '.' or len(s)==0:
continue
elif s == '..':
if len(stack)!=0:
stack.pop()
else:
stack.append(s)
res = ""
if len(stack)==0:
return "/"
for i in stack:
res += "/"+i
return res

Python Code

总耗时: 450 ms

lintcode 中等题: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. 【LeetCode每天一题】Simplify Path(简化路径)

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

  3. [LeetCode] Simplify Path 简化路径

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

  4. 071 Simplify Path 简化路径

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

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

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

  6. Leetcode71. Simplify Path简化路径

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

  7. Simplify Path(路径简化)

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

  8. lintcode 中等题: Implement Trie

    题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例   注意 You may assu ...

  9. lintcode 中等题:k Sum ii k数和 II

    题目: k数和 II 给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字. 在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案. 样例 ...

随机推荐

  1. [转]Linux 分区 swap

    如何合理设置Linux的swap分区 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://commandos.blog.51cto.c ...

  2. WCF 服务的ABC之地址(五)

    地址 Address 在WCF中,每个服务都有一个唯一的地址(Address). 地址包含两个重要的元素:服务位置及传输协议. 服务位置包含目标机器名.站点.通信端口.管道(或队列),以及一个可选的特 ...

  3. openerp学习笔记 webkit 打印

    1.webkit 打印需要安装的支持模块 请首先安装 Webkit 报表引擎(report_webkit),再安装 Webkit 报表的支持库(report_webkit_lib),该模块讲自动安装和 ...

  4. Azure IaaS for IT Pros Online Event 总结

    微软一个为期4天的一个有关于Azure的介绍,主要总结了些Azure现有的技术以及将会推出东西 主题链接 http://channel9.msdn.com/Events/Microsoft-Azure ...

  5. 自己实现的库函数1(strlen,strcpy,strcmp,strcat)

    为了便于理解和使用库函数,先把自己实现的几个函数以及测试函数呈现如下. //求字符串长度的函数int my_strlen(const char* pStr){ assert(pStr != NULL) ...

  6. 10、WPF程序集

    WPF核心程序集 PresentationCore.dll:这个程序集定义了许多构成WPF GUI层基础的类型.例如包含WPF Ink API(pc笔针输入,手写输入)的支持.几个动画基元以及几个图形 ...

  7. 【Go】 格式处理

    格式化字符串 在golang里面获取时间戳并不难.只要加载time包.time.Now().Unix() fmt.Sprintf("%d",int64),自此,go语言的int转换 ...

  8. 博主教你制作类似9patch效果的iOS图片拉伸

    下面张图片,本来是设计来做按钮背景的:   button.png,尺寸为:24x60 现在我们把它用作为按钮背景,按钮尺寸是150x50: // 得到view的尺寸 CGSize viewSize = ...

  9. 收起虚拟键盘的各种方法 -- IOS

    使用虚拟键盘来输入资讯,是 iOS 的重要互动方式之一,虚拟键盘通常会自动出现在可以编辑的 UITextField 或是 UITextView 的编辑事件中,叫出键盘固然容易,但是要把它收起来,可就没 ...

  10. 敏捷开发之道(二)极限编程XP

    上次的博文敏捷开发之道(一)敏捷开发宣言中,我们介绍了一下敏捷开发宣言,在其中,我们了解到了关于敏捷开发的几个重要的价值观.今天我们来了解一个敏捷开发的方法--极限编程XP 1.介绍 极限编程(eXt ...