题目

简化路径

给定一个文档(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. git 的使用(4)-git暂缓区工作区原理和修改删除等命令

    文章转载自:http://blog.csdn.net/think2me/article/details/39056379 博主说未经本人允许,不得转载,那就不贴了,拷贝关键部分作备忘 1. 暂存区是G ...

  2. js----深入理解闭包

    闭包算是js里面比较不容易理解的点,尤其是对于没有编程基础的人来说. 其实闭包要注意的就那么几条,如果你都明白了那么征服它并不是什么难事儿.下面就让我们来谈一谈闭包的一些基本原理. 闭包的概念 一个闭 ...

  3. php后台如何避免用户直接进入方法实例

    这篇文章介绍了php后台如何避免用户直接进入方法实例,有需要的朋友可以参考一下 1)创建BaseController控制器继承Controller(后台的一切操作要继承BaseController): ...

  4. vc列表控件的初始化

    void CManageProcessDlg::InitList() {  m_ListProcess.SetExtendedStyle(m_ListProcess.GetExtendedStyle( ...

  5. 单例模式C#

    首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...

  6. 图解 CSS: 理解样式表的逻辑(转载)

    原文:http://www.cnblogs.com/del/archive/2009/02/01/1382141.html 样式表可以是外部的.内联的或嵌入的; 链接外部样式文件一般是:<lin ...

  7. MySQL 多实例启动和关闭脚本

    DB: 5.5.14 OS:CentOS 6.3 在MySQL多实例中,一个一个启动灰常麻烦,因此写个简单的启动和关闭脚本 启动脚本:start.sh #!/bin/bash for port in ...

  8. C# 将cookiecontainer写到本地

    public static void WriteCookiesToDisk(string file, CookieContainer cookieJar) { using(Stream stream ...

  9. entityframwork

    entityframwork映射部分: public class NorthwindContext : DbContext { public DbSet<CATEGORIES> Categ ...

  10. 如何调优JVM - 优化Java虚拟机(大全+实例)

    堆设置 -Xmx3550m:设置JVM最大堆内存 为3550M. -Xms3550m:设置JVM初始堆内存 为3550M.此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存. -X ...