lintcode 中等题:Simplify Path 简化路径
题目
简化路径
给定一个文档(Unix-style)的完全路径,请进行路径简化。
"/home/", => "/home"
"/a/./b/../../c/", => "/c"
你是否考虑了 路径 =
"/../"的情况?在这种情况下,你需返回
"/"。此外,路径中也可能包含双斜杠
'/',如"/home//foo/"。在这种情况下,可忽略多余的斜杠,返回
"/home/foo"。
解题
linux 没碰过,真的表示不知道这个是什么鬼
路径简化的依据是:
当遇到“/../"则需要返回上级目录,需检查上级目录是否为空。
当遇到"/./"则表示是本级目录,无需做任何特殊操作。
当遇到"//"则表示是本级目录,无需做任何操作。
当遇到其他字符则表示是文件夹名,无需简化。
当字符串是空或者遇到”/../”,则需要返回一个"/"。
当遇见"/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 简化路径的更多相关文章
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- 【LeetCode每天一题】Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- lintcode 中等题: Implement Trie
题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assu ...
- lintcode 中等题:k Sum ii k数和 II
题目: k数和 II 给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字. 在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案. 样例 ...
随机推荐
- asp.net mvc常用的数据注解和验证以及entity framework数据映射
终于有时间整理一下asp.net mvc 和 entity framework 方面的素材了. 闲话少说,步入正题: 下面是model层的管理员信息表,也是大伙比较常用到的,看看下面的代码大伙应该不会 ...
- Delphi 两个应用程序(进程)之间的通信
两个应用程序之间的通信实际上是两个进程之间的通信.由于本人知识有限,决定应用消息来实现.需要用到的知识: 1.RegisterWindowMessage(); //参数类型:pchar:返回值:Lon ...
- c# datatable list 相互转换
/*Converts List To DataTable*/ public static DataTable ToDataTable<TSource>(IList<TSource&g ...
- java dom4j解析xml用到的几个方法
1. 读取并解析XML文档: SAXReader reader = new SAXReader(); Document document = reader.read(new File(fileName ...
- ActiveMQ之Queue
Queue实现的是点到点模型,在以下的例子中,启动2个消费者共同监听一个Queue,然后循环给这个Queue发送多个消息. 代码如下: public class QueueTest { /** * @ ...
- Oracle使用%type类型的变量输出结果
使用%type关键字可以声明一个与指定列名称相同的数据类型,他通常紧跟在指定列名的后面. 使用%type的2个好处: 1.用户不必查看表中各个列的数据类型,就可以确保所定义的变量能够存储检索的数据. ...
- 删除或清空具有外键约束的表数据报-ERROR 1701 (42000)
OS: centos 6.3 DB:5.5.14 mysql> select database();+------------+| database() |+------------+| sa ...
- plateform_driver_register和plateform_device_register区别
设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册时进行绑定. 以一个USB设备为例,有两种情形: (1)先插上USB设备并挂到总线中,然后在安装USB驱动程序过程中从总线上遍历各个设备,看驱 ...
- tcp-client-c++
#include "stdafx.h" #include <Winsock2.h> #include <iostream> #pragma comment( ...
- always语言指导原则
1.每个always只有一个@(event-expression). 2.always块可以表示时序逻辑和组合逻辑. 3.带有posedge和negedge关键字的是表示沿触发的时序逻辑,没有的表示组 ...