一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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、“////”多个/当一个处理

在这里,我们可以利用stack来处理,“..”则把栈顶元素弹出,“.”则不作处理,其他的表示目录的名字就直接压栈。

具体思路可以看代码:

class Solution {
public:
    string simplifyPath(string path) {
        stack<string> pathStack;//存放目录
        path+="/";//避免处理边界,即/a这种情况
        string temp = "";//用作临时string
        for(int i = 0 ; i< path.length() ;)
        {
            if(path[i] == '/')
            {
                if(temp=="..") {//返回上一层目录
                    if(!pathStack.empty()) pathStack.pop();//如果非空则弹出,为空就不做处理
                }
                else if(temp=="."|| temp =="") {}//不做处理,当前目录
                else  pathStack.push(temp);//正常的目录
                while(i<path.length()&&path[i]=='/') i++;//跳过连续的‘/’
                temp = "";//temp初始化
            }
            else
            {
                temp+=path[i++];
            }
        }
        string ret;
        if(pathStack.empty()) ret = "/";//为空时直接输出
        else {
            while(!pathStack.empty())//非空则按格式输出
            {
                string eachPath = pathStack.top();
                pathStack.pop();
                ret = "/"+eachPath + ret;
            }
        }
        return ret;
    }
};

【一天一道LeetCode】#71. Simplify Path的更多相关文章

  1. [LeetCode] 71. Simplify Path 简化路径

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

  2. Leetcode#71 Simplify Path

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

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

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

  4. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 71. Simplify Path(M)

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

  6. 【LeetCode】71. Simplify Path

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

  7. 【一天一道LeetCode】#112. Path Sum

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【leetcode】Simplify Path

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

  9. Java for LeetCode 071 Simplify Path

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

随机推荐

  1. css文本超出省略号

    终于完成了项目了,今天我就分享自己在项目中比较实用的一些功能的实现,第一个就是纯css文本超出省略号 /* 文本单行超出省略号 */ .textels { overflow: hidden; text ...

  2. spring web项目下,判断项目是否启动完成

    本文同时发布于见鬼网:https://faceghost.com/article/483341 概述:spring 加载完成后,我们有时候会做一些初始化工作,如加载一些缓存,DB,等,这里采用实现Se ...

  3. 基于无域故障转移群集 配置高可用SQLServer 2016数据库

    基于上次的文章搭建的环境,可以在这里:http://www.cnblogs.com/DragonStart/p/8275182.html看到上次的文章. 演示环境 1. 配置一览 Key Value ...

  4. 高仿腾讯QQ最终版

    之前写过一篇关于高仿腾讯QQ的博客,不知道的看这:http://blog.csdn.net/htq__/article/details/51840273 ,主要是从界面上高仿了腾讯QQ,在UI上基本上 ...

  5. Openstack: MP-BIOS bug: 8254 timer not connected to IO-APIC

    Issue: After you import an linux image into openstack and run an instance of it, you may find that t ...

  6. 【我的书】《Unity Shader入门精要》出版上市

    重要的事 先说重要的事,就是我的书籍<Unity Shader入门精要>在经过无数次跳票后,终于出版上市了(泪目-)! 购买传送门: 亚马逊 当当 京东 截止到我写这篇文章的时候,京东是没 ...

  7. Swift:Foundation框架中的NS前缀的由来

    可能大家对于著名的NS前缀的由来有一些疑问. 绝大多数这些NS前缀的类是NeXTSTEP操作系统中Foundation框架里的一部分,而该操作系统是OS X的基础. NeXTSTEP的程序员对它们的类 ...

  8. iOS学习笔记--触摸事件

    最近空闲时间在学习iOS相关知识,几周没有更新文章了,今天总结下这些天的学习内容,也整理下iOS的学习笔记,以便以后查阅翻看- iOS中的事件可以分为3大类型: 触摸事件 加速计事件 远程控制事件 响 ...

  9. How Do I Declare A Block in Objective-C? [备忘]

    How Do I Declare A Block in Objective-C? As a local variable: returnType (^blockName)(parameterTypes ...

  10. 使用Contacts Contract Content Provider操作通讯录最佳实践

    Android向所有被赋予READ_CONTACTS权限的应用程序提供了联系人信息数据库的完全访问权限.Contacts Contract使用3层数据模型去存储数据,下面介绍Contacts Cont ...