LeetCode-Simplify Path

题目要求:
路径简化
Example:

path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
path = "/a/../../b/../c//.//", => "/c"
path = "/a//b////c/d//././/..", => "/a/b/c"

最主要的是边界情况的处理:
处理思路:
利用vector存储每个有效路径单元如"/var/log/exm/"->/var /log /exm,遍历路径,存储依次存储,遇上'//' '/.'等直接忽略,'/..'则删除vector末尾的路径。
边界情况:
最后一个字符是否是'/'
以及vector为空时,遇到/..如何处理

源码如下:

class Solution {
public:
    string simplifyPath(string path) {
        vector word;
        int pre = 0;
        int pos = 0;
        string ans;
        for (int index=1;index

你可能感兴趣的:(LeetCode-Simplify Path)