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. javascript数据结构与算法---栈

    javascript数据结构与算法---栈 在上一遍博客介绍了下列表,列表是最简单的一种结构,但是如果要处理一些比较复杂的结构,列表显得太简陋了,所以我们需要某种和列表类似但是更复杂的数据结构---栈 ...

  2. ASP.NET 对于文件的下载与上传

    /// <summary> /// 下载附件查看 /// </summary> /// <param name="sender"></pa ...

  3. js中apply()和call()方法的使用

    1.apply()方法 apply方法能劫持另外一个对象的方法,继承另外一个对象的属性.  Function.apply(obj,args)方法能接收两个参数     obj:这个对象将代替Funct ...

  4. mysql数据表拷贝

    select * into 目标表名 from 源表名 insert into 目标表名(fld1, fld2) select fld1, 5 from 源表名 以上两句都是将 源表 的数据插入到 目 ...

  5. PHP PDO的setAttribute函数

    数据库的连接: $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=cardslg'; $username = 'root'; $password = ''; ...

  6. 未备案域名打开国内服务器上的网站(绑定国外空间并判断url后跳转引用)

    场景:由于域名没备案不能绑定国内服务器,通过先绑定国外空间,在空间着陆页判断当前url,打开不同的页面.页面上通过iframe引用国内服务器上的目标网站. 实现:未备案域名打开国内服务器上的网站. 国 ...

  7. 【DevOps】DevOps成功的八大炫酷工具

    为自动化和分析所设计的软件及服务正加速devops改革的步伐,本文为你盘点了Devops成功的八大炫酷工具 Devops凭借其连接弥合开发与运营团队的能力正在各个行业呈现席卷之势.开发人员和运营人员历 ...

  8. C# 文件/文件夹重命名

    C# 重命名的方法是MoveTo() 官方文档地址 (https://msdn.microsoft.com/zh-cn/library/system.io.fileinfo.moveto%28VS.8 ...

  9. 网页Screen width、height、availWidth、availHeight属性

    *screen.width 功能:声明了显示浏览器的屏幕的宽度,以像素计. 语法:screen.width *screen.height 功能:声明了显示浏览器的屏幕的可用宽度,以像素计. 语法:sc ...

  10. 在SQLSERVER2008中建立数据库复制碰到的问题

    一是开始用FTP快照方式,设置好后运行中无法传输快照,应该是FTP设置中的问题,有待进一步研究.后改用文件夹共享方式,出现无法取得文件的错误,原因是订阅服务器上的快照文件夹设为默认设置,改成设置为备用 ...