zoukankan      html  css  js  c++  java
  • Simplify Path

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

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

    click to show corner cases.

    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".
     
    把‘/’当做字符串之间的区分,然后分析字符串便可
    .忽略
    ..回退
    其它则全部按符号处理
     
    class Solution {
    public:
        string simplifyPath(string path) {
            string re = "";
            stack <string > sk;
            int i = 0;
            int len = path.length();
            while(i<len)
            {
                if(path[i] == '/')
                {
                    i++;
                    continue;
                }
                int j = 0;
                
                while(i+j < len && path[i+j]!= '/')j++;
                
                string temp = path.substr(i,j);
                
                if(temp == "..")
                {
                    if(!sk.empty())
                    sk.pop();
                    i = i+2;
                }
                else if(temp == "."){
                    i++;
                    continue;
                    }
                else
                {
                    i = i +j;
                    sk.push(temp);
                }
                
            }
            if(sk.empty())return "/";
            while(!sk.empty())
            {
                string temp = sk.top();
                re = '/' + temp + re;
                sk.pop();
            }
            return re;
        }
    };
    

      

  • 相关阅读:
    node 搭建代理服务器
    jquery常见的方法
    静态布局字体标签
    ajax简单了解
    GET方式缓存清除
    Ajax使用概述
    SESSION技术
    COOKIE技术
    PHP操作数据库(二)-增删改查操作
    PHP操作数据库(一)-步骤介绍
  • 原文地址:https://www.cnblogs.com/pengyu2003/p/3596269.html
Copyright © 2011-2022 走看看