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".

解题:

题意为简化unix路径,即省略多余字符“/”和运算“.”、"..",返回unix的真实路径;

需要考虑的特殊情况是:

1、根目录下操作"..",还是根目录

2、连续两个“/”,可看做无任何作用

3、路径末尾的"/",要消除

思路:

每次取出两个“/”之间的字符,如果是"."或者空字符表示还在本层路径,如果是".."表示返回上一层路径,如果是其他字符,则表示下一层路径名;

用栈来保存每一层路径非常合适,但是后来变成过程中,使用vector来模拟栈操作,在最后整合最终路径时反而更方便。

代码:

 class Solution {
public:
string simplifyPath(string path) {
string res, substr;
vector<string> stk;
stringstream ss(path); while(getline(ss, substr, '/')) {
if (substr == ".." and !stk.empty())
stk.pop_back();
else if (substr != ".." && substr != "." && substr != "")
stk.push_back('/' + substr);
} for (int i = ; i < stk.size(); ++i)
res += stk[i]; return res.empty() ? "/" : res;
}
};

【Leetcode】【Medium】Simplify Path的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

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

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

  6. 【leetcode刷题笔记】Simplify Path

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

  7. 【LeetCode每天一题】Minimum Path Sum(最短路径和)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. 【leetcode刷题笔记】Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. 安装TD出现Unknown user name or bad password问题

    在Server 2003 sp2上安装TD8.0 出现Unknown user name or bad password,是因为2003启用了DEP保护.  关闭系统的DEP保护就可以了.  方法如下 ...

  2. ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash

    rails 开发中 5.1版本使用binding.pry会报 ActionController::UnfilteredParameters: unable to convert unpermitted ...

  3. Unix/Linux文件类型及访问权限

    在Linux系统中,有7种文件类型. 普通文件 (regular file) 目录文件 (directory) 链接文件 (symbolic link) 管道文件 (FIFO) 套接字文件 (sock ...

  4. minStack实现

    设计包含 min 函数的栈(栈)定义栈的数据结构,要求添加一个 min 函数,能够得到栈的最小元素.要求函数 min.push 以及 pop 的时间复杂度都是 O(1). #include <a ...

  5. 16G的U盘 4G的压缩

    文件系统格式原因,或是你的U盘是扩容盘(就是实际容量和显示的不一样)常用文件系统支持的单个文件大小: FAT16 支持单个文件最大不超过2GB FAT32 支持单个文件最大不超过4GB(有人说实际超过 ...

  6. iOS 性能优化套路

    ***  一级套路 ***  使用ARC管理内存- 防止内存泄露- 保证释放掉不再需要的内存,提高性能 在正确的地方使用 reuseIdentifier平时接触的需要考虑重用的视图有UICollect ...

  7. jsonp全国天气案例

    案例1: 1.获取跨域数据 2.将数据按照下面的效果放到body里面     key: f49570d39b02b3c203526b5d8255aa61 079179afb105ce2bae9f5d0 ...

  8. [转]Sql Server 分页存储过程

    本文转自: 版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking  本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且 ...

  9. 事件绑定的快捷方式 利on进行事件绑定的几种情况

    [事件绑定快捷方式]$("button:first").click(function(){ alert(1); }); [使用on绑定事件] ① 使用on进行单事件绑定 $(&qu ...

  10. LINQ-from多from

    简: LINQ全称是Language  Integrated Query,中文“语言集成查询”.LINQ是一种查询技术,有LINQ toSQL.LINQ to Object. LINQ to ADO. ...