zoukankan      html  css  js  c++  java
  • [LeetCode]71 Simplify Path(模拟)

    题目链接:https://leetcode.com/problems/simplify-path/?tab=Description

    题意:化简一个绝对路径。

     1 class Solution {
     2 public:
     3     string simplifyPath(string path) {
     4         string s;
     5         int begin = 0, len = 0;
     6         for(int i = 1; i <= path.length(); i++){
     7             if(!begin && path[i] != '/') {
     8                 begin = i;
     9             }
    10             else if(begin && (path[i] == '/' || i == path.length())) {
    11                 len = i - begin;
    12             }
    13             if(len) {
    14                 string ns = path.substr(begin, len);
    15                 begin = len = 0;
    16                 if(!ns.compare(".")) {
    17                     continue;
    18                 } 
    19                 else if(!ns.compare("..")) {
    20                     int j(s.length() - 1);
    21                     while(s[j] != '/') j--;
    22                     s = s.substr(0, j);
    23                 }
    24                 else s += '/' + ns;
    25             }
    26         }
    27         return s.length() ? s : "/";
    28     }
    29 };
  • 相关阅读:
    函数配接器
    函数对象和函数指针
    unary_function 和 binary_function
    堆排序
    Shell排序
    volatile理解
    死锁
    进程间通信
    优化循环的方法-循环展开
    如何学习编译原理
  • 原文地址:https://www.cnblogs.com/kirai/p/6530405.html
Copyright © 2011-2022 走看看