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

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

Corner Cases:

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".

规律:

1、".."表示跳到上一层目录,删掉它上面挨着的一个路径。

2、"."表示当前目录,直接去掉。

3、如果有多个"/"只保留一个。

4、如果最后有一个"/",去掉不要。

Java:

public class Solution {
public String simplifyPath(String path) {
java.util.LinkedList<String> stack = new java.util.LinkedList<String>();
java.util.Set<String> set = new java.util.HashSet<String>(java.util.Arrays.asList("..", ".", ""));
for (String str : path.split("/")){
if (str.equals("..") && !stack.isEmpty()) stack.pop();
if (!set.contains(str)) stack.push(str);
} String result = "";
for (String str : stack) result = "/" + str + result;
return result.isEmpty() ? "/" : result;
}
}  

Python:

class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
stack, tokens = [], path.split("/")
for token in tokens:
if token == ".." and stack:
stack.pop()
elif token != ".." and token != "." and token:
stack.append(token)
return "/" + "/".join(stack)

C++:

class Solution {
public:
string simplifyPath(string path) {
vector<string> v;
int i = 0;
while (i < path.size()) {
while (path[i] == '/' && i < path.size()) ++i;
if (i == path.size()) break;
int start = i;
while (path[i] != '/' && i < path.size()) ++i;
int end = i - 1;
string s = path.substr(start, end - start + 1);
if (s == "..") {
if (!v.empty()) v.pop_back();
} else if (s != ".") {
v.push_back(s);
}
}
if (v.empty()) return "/";
string res;
for (int i = 0; i < v.size(); ++i) {
res += '/' + v[i];
}
return res;
}
};

All LeetCode Questions List 题目汇总

[LeetCode] 71. 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. lintcode 中等题:Simplify Path 简化路径

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

  3. [LeetCode] Simplify Path 简化路径

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

  4. 【LeetCode每天一题】Simplify Path(简化路径)

    Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...

  5. 071 Simplify Path 简化路径

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

  6. Leetcode71. Simplify Path简化路径

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

  7. Leetcode#71 Simplify Path

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

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

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

  9. 71. Simplify Path(M)

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

随机推荐

  1. kafka安装测试报错 could not be established. Broker may not be available.

    修改 config 下配置文件 vim server.properties 配置本机ip listeners=PLAINTEXT://192.168.174.128:9092 执行命令时 bin/ka ...

  2. The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple (Mirror)

    B题 思路 因为 \[ x=\sum\limits_{k=1}^{n}ka_k\\ y=\sum\limits_{k=1}^{n}ka_{k}^{2} \] 我们设交换前和交换后的这两个等式的值设为\ ...

  3. 如何使用git,进行项目的管理

    1.首先,现在git上个创建一个项目, 2.然后在本地创建一个springboot工程 3.使用git命令   git init 将这个springboot项目交给git进行管理 4.创建一个dev分 ...

  4. Alpha冲刺(10/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

  5. 网络基础知识(http请求)

    http请求的过程 域名解析----TCP连接 ----发送请求-----响应请求----获取html代码----浏览器渲染 TCP是主机对主机层的控制传输协议,提供可靠的连接服务 TCP的三次握手 ...

  6. (4)ardunio 矩阵求解官方库改造,添加逆的求解

    多此一举,原来官方库给了求逆的函数,在源码里 除此之外,还有转置矩阵,只不过样例没显示出来. //Matrix Inversion Routine // * This function inverts ...

  7. Impala 介绍(转载)

    一.简介 1.概述 Impala是Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能. •基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等 ...

  8. learning scala akka tell pattern(二)

    package com.example import akka.actor._ object Tutorial_02_Tell_Pattern extends App { println(" ...

  9. WinDbg常用命令系列---.write_cmd_hist (写命令历史记录)

    .write_cmd_hist 简介 .write_cmd_hist命令将调试器命令窗口的整个历史记录写入文件. 使用形式 .write_cmd_hist Filename 参数 Filename指定 ...

  10. isntall

    #! /bin/bash # zabbix server address zabbix='10.10.238.110' # repo address REPO='10.10.238.114:4507' ...