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;}
}

转载于:https://www.cnblogs.com/bingtel/p/4194412.html

Simplify Path相关推荐

  1. LeetCode 71. Simplify Path

    LeetCode 71. Simplify Path 本博客转载自:https://blog.csdn.net/makuiyu/article/details/44497901 Solution1:没 ...

  2. [Leetcode] Simplify Path

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

  3. leetcode 79:simplify path

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

  4. [LintCode] Simplify Path [字符串操作]

    Problem Given an absolute path for a file (Unix-style), simplify it. Example "/home/", =&g ...

  5. [LeetCode]--71. Simplify Path

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

  6. 71. Simplify Path

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

  7. LeetCode Simplify Path(栈操作)

    题意:给出一个文件路径,输出简化后的文件路径 如/home/ => /home /.. => / /. => / 思路:当遇到/时,就将字符串压入栈中,同时将字符串清空.在遇到..时 ...

  8. leetcode解题文件夹

    点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...

  9. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

  10. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

最新文章

  1. 面试腾讯算法:组合总和
  2. 大朗机器人餐厅在哪里_东莞餐厅惊现机器人服务员 平均每个10万元
  3. 在CesiumVR基础上实现3D左右立体视觉
  4. 【渝粤题库】国家开放大学2021春2143西方经济学题目
  5. java派生类_我可以使派生类从Java的基类继承派生成员...
  6. 信息学奥赛C++语言:分糖果
  7. php电影票在线选座,适用于电影院、机票、汽车票在线选座位插件jQuery-Seat-Charts...
  8. 2022届美团和腾讯校招薪资出来了,被倒挂了。。。
  9. cdn搭建原理_CDN运行过程四大要点,附技术原理图解,智能DNS解析
  10. 淘宝/天猫API:upload_img-上传图片到淘宝
  11. matlab处理fits文件,读取 FITS 文件中的数据
  12. 【Proteus仿真】TLC5615输出256点正弦波(振幅可调)
  13. C#未能加载文件或程序集
  14. 【MRCPv2协议介绍】 Generic Message Headers
  15. Xmind软件 2020最新安装教程讲解
  16. 机器学习基石-05-3-Effective Number of Hypotheses
  17. ibm服务器 产品型号对应表,IBM服务器配件型号及编号列表
  18. Linux笔记——软件包管理
  19. windows自带截图键(shift+ win + s)没有反应,一招教你快速解决!
  20. ARM树莓派高级开发——linux内核源码、树莓派源码编译、SD卡挂载

热门文章

  1. 吸烟首先危害是会引起火灾,电子烟较好
  2. 是否可以利用光压提高卫星轨道
  3. gstreamer之RTSP Server一个进程提供多路不同视频
  4. Bridge(桥模式)
  5. brew更新的时候不更新某个应用_可以不可以第六集剧情介绍,日剧可以不可以什么时候更新...
  6. java 快速创建map_快速创建Map并初始化,超级简单
  7. mysql sumif优化,sumif函数的使用方法
  8. Unicode字符集下UTF-8 、UTF-16编码和GBK(GB2312)字符集
  9. mysql获取变量_获取Mysql的状态、变量
  10. php 网址尾部带斜杠和不带区别,URL中带斜杠和不带斜杠的区别 | 米莱SEO博客