71.Simplify Path

来源:互联网 发布:中老年女式毛衣淘宝 编辑:程序博客网 时间:2024/06/03 20:26

71.Simplify Path

  • 题目描述:Given an absolute path for a file (Unix-style), simplify it.

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

  • 题目含义:简化path

  • 代码

    package String;import java.util.*;/*** @Author OovEver* @Date 2017/12/9 16:05*/public class LeetCode71 {  public String simplifyPath(String path) {      Deque<String> stack = new LinkedList<>();      Set<String>   skip  = new HashSet<>(Arrays.asList("..", ".", ""));      for (String dir : path.split("/")) {          if (dir.equals("..") && !stack.isEmpty()) stack.pop();          else if (!skip.contains(dir)) stack.push(dir);      }      String res = "";      for (String dir : stack) res = "/" + dir + res;      return res.isEmpty() ? "/" : res;  }}