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个数的和等于目标数字,你需要找出所有满足要求的方案. 样例 ...
随机推荐
- winform INI文件操作辅助类
using System;using System.Runtime.InteropServices;using System.Text; namespace connectCMCC.Utils{ // ...
- ORACLE 语句关联统计
很久不用SQL语句了,貌似入职新公司后,又回归到了三年前的SQL时代,一写一坨的SQL好吧,也当回归一下过去的知识. 下面是统计2月份某数据的计费统计 select t.telno as 主号,VID ...
- 例题6-5 Boxes in a line uVa12657
这道题目的解决方案是双向链表,数据结构本身并不复杂,但对于四种情况的处理不够细致,主要体现在以下几点: 分类讨论不全面,没有考虑特殊情况(本身不需要操作,需要互换的两元素相邻) 没有考虑状态4改变后对 ...
- arguments.callee 调用函数自身用法----JSON.parse()和JSON.stringify()前端js数据转换json格式
arguments.callee 调用函数自身用法 arguments.callee 在哪一个函数中运行,它就代表哪个函数. 一般用在匿名函数中. 在匿名函数中有时会需要自己调用自己,但是由于是匿名函 ...
- fastclick插件 导致 input[type="date"] 无法触发问题解决方案
鄙人才疏学浅,新人一枚,不足之处还请谅解,写下这个也只是为了给大家分享一下我解决这个BUG的方法,也是自己的一个笔记. 首先,我们使用fastclick插件的初衷是解决“tap”事件“点透”的BUG: ...
- js拖拽3D立方体旋转
这段时间有点闲,所以就自己找些小玩意来练习练习.今天做了一个可以拖拽页面内空白位置3D立方体就会跟着转动的小例子,布局方面用到css3 3D转换技术,通过transform控制旋转实现的. 上个图 代 ...
- Delphi XE5教程5:程序的结构和语法
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...
- hadoop启动后jps 没有namenode
hadoop集群启动后,jps 发现没有namenode. 网上查了下,这问题可能是由于两次或两次以上格式化NameNode造成的. 因为我的是刚刚新搭建的集群,里面没什么资料,所以我直接删除各个 ...
- Spark Standalone运行过程
以下内容参考http://www.cnblogs.com/luogankun/p/3912956.html 一.集群启动过程--启动Master 二.集群启动过程--启动WorkerWorker运行时 ...
- centos crontab 定时任务详解
安装crontab: yum install crontabs 说明: /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 ...