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

For example,

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

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

算法:根据/把path分割,再逐个判断

public class Solution {
public String simplifyPath(String path) {
String[] strs = path.split("/");
Stack<String> s = new Stack<String>();
for(int i=0;i<strs.length;i++){
if(strs[i].equals(".")){
}else if(strs[i].equals("..")){
if(!s.isEmpty())
s.pop();
}else if(strs[i].length()>0){
s.push(strs[i]);
}
}
StringBuilder sb = new StringBuilder();
Iterator<String> it = s.iterator();
while(it.hasNext()){
String t = it.next();
sb.append("/").append(t);
}
String r=sb.toString();
if(r.length()==0)
r+="/";
return r;
}
}

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. 56. Edit Distance && Simplify Path

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...

  3. leetcode面试准备:Simplify Path

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

  4. 71. Simplify Path(M)

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...

  5. 【LeetCode】71. Simplify Path

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

  6. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. [LeetCode] Simplify Path 简化路径

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

  8. 【leetcode】Simplify Path

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

  9. Leetcode Simplify Path

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

随机推荐

  1. 2015.4.25-2015.5.1 字符串去重,比例圆设计,中奖机和canvas橡皮擦效果等

    1.字符串去重,html模板取值   2.javascript正则表达式之$1...$9   3.jquery插件   4.返回上一页并刷新 解决方法: <a href ="javas ...

  2. 【bzoj1076】[SCOI2008]奖励关

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...

  3. python操作mysql总结

    Windows系统,python环境搭建. 下载并安装python2.7.11 https://www.python.org/downloads/ 下载并安装python的mysql包: http:/ ...

  4. java笔记--关于线程同步(7种同步方式)

    关于线程同步(7种方式) --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3897440.html"谢谢-- 为何要使用同步? ...

  5. SQL语句 分页实现

    1 通过sql实现分页. select top 5 * from judgeorder where id  not in (select top 10 id from judgeorder order ...

  6. windows网络编程

    1.协议的特征 面向消息的和基于流的 面向连接的和面向无连接的 2.端口分为三类:“已知”端口.已注册端口.动态和私用端口 0~1023 IANA控制,为固定服务保留的 1024~49151 已注册端 ...

  7. VS2013无法连接到SqlServer的问题解决

    在本机安装Vs2013后,安装Sqlserver2012数据库,在VS开发时,数据库一直查询不到 点击刷新后,看不见本机Sql服务器 最初检查防火墙设置,发现添加sqlservr.exe依然不起作用 ...

  8. realloc的使用误区

    C语言 realloc() 函数位于 stdlib.h 头文件中,原型为: void *realloc(void *ptr, size_t size); realloc() 会将 ptr 所指向的内存 ...

  9. MVC 前台获取三级菜单及子菜单

    1.在后台将所有的菜单获取出来,代码如下: public ActionResult Index() { //所有商品分类 var oneMenu = _baseGoodsCategory.FindLi ...

  10. 站点SEO优化日记之设置目标关键词

    一.怎样确定关键词? 根据百度指数,选择搜索指数较高的作为候选关键词.一个关键词的搜索结果中,若大部分都为非首页链接,则该关键词可在短时间内通过优化获得好的排名,可选择此类关键词作为目标关键词. 二. ...