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

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

解题思路:

首先要了解linux目录的意思,请参考Linux的文件管理

先把path按" /" split,建立一个栈,如果出现..则出栈,否则入栈,最后输出这个栈即可,JAVA实现如下:

    public String simplifyPath(String path) {
Stack<String> res = new Stack<String>();
String[] ps = path.split("/");
for (String a : ps) {
if (a.equals("..")) {
if (!res.empty())
res.pop();
}
else if (a.equals(".") || a.equals(""))
continue;
else
res.push(a);
}
if (res.empty())
return "/";
StringBuilder sb = new StringBuilder();
while(!res.empty())
sb.append("/" + res.get(i));
return sb.toString();
}

Java for LeetCode 071 Simplify Path的更多相关文章

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

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

  2. Leetcode 之Simplify Path @ python

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

  3. 071 Simplify Path 简化路径

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

  4. 【leetcode】Simplify Path

    题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...

  5. Java for LeetCode 064 Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

  7. leetcode[70] Simplify Path

    题目的意思是简化一个unix系统的路径.例如: path = "/home/", => "/home"path = "/a/./b/../../ ...

  8. Leetcode 之Simplify Path(36)

    主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...

  9. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

随机推荐

  1. note.js之 Mongodb在Nodejs上的配置及session会话机制的实现

    上篇我们使用nodejs实现了一个express4的网站构建配置,但一个有面的网站怎么可以缺少一个数据库呢.现在较为流行的就是使用MONGODB来作为nodejs网站引用的数据库,可能它与nodejs ...

  2. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

  3. codeforces 288A:Polo the Penguin and Strings

    Description Little penguin Polo adores strings. But most of all he adores strings of length n. One d ...

  4. POJ1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  5. Codeforces 556D Restructuring Company

    传送门 D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. pthread_kill

    别被名字吓到,pthread_kill可不是kill,而是向线程发送signal.还记得signal吗,大部分signal的默认动作是终止进程的运行,所以,我们才要用signal()去抓信号并加上处理 ...

  7. 看看这些JavaScript题目你会做吗?

    题目1 咋一看这题目,还以为答案选择B呢,其实正确答案为D,知道原因吗?接着往下看 map对数组的每个元素调用定义的回调函数并返回包含结果的数组,咋一看还以为它会像如下这样执行: function t ...

  8. c++ 类型安全

    类型安全很大程度上可以等价于内存安全,类型安全的代码不会试图访问自己没被授权的内存区域.“类型安全”常被用来形容编程语言,其根据在于该门编程语言是否提供保障类型安全的机制:有的时候也用“类型安全”形容 ...

  9. 初学Hibernate

    Hibernate 是完全ORM的,只需要对 对象 进行操作,生成底层SQL语句 优势:1.可以简化开发 2.性能好(原生的Hibernate性能很差,要使用它,需要进行优化),优化方式:一级缓存.二 ...

  10. Ten Tips for Writing CS Papers, Part 1

    Ten Tips for Writing CS Papers, Part 1 As a non-native English speaker I can relate to the challenge ...