`
cozilla
  • 浏览: 92137 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[LeetCode] Simplify Path

 
阅读更多

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

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

 

字符串最麻烦~折腾

class Solution {
public:
    void getnext(const string& str, int& start, int& end) {
        while (start < str.size() && str[start] == '/') start++;
        if (start == str.size()) {start =-1; return;}
        end = start;
        while (end < str.size() && str[end] != '/') end++;
    }

    string simplifyPath(string path) {
        char* a = new char[path.size()];
        memset(a, 0, path.size());
        int pos = 0, end = 0;
        
        while (end < path.size()) {
            getnext(path, pos, end);
            if (pos == -1) break;
            int l = end - pos;
            string sub = path.substr(pos, l);
            if (sub.compare(".") == 0) {
            } else if (sub.compare("..") == 0) {
                int last = strlen(a) - 1;
                while (last >= 0 && a[last] != '/') last--;
                if(last < 0) a[0] = '\0';
                else a[last] = '\0';
            } else {
                strcat(a, "/");
                strcat(a, sub.c_str());
            }
            pos = end + 1;
        }
        if (strlen(a) == 0) {a[0] = '/'; a[1] = '\0';}
        string res(a);
        delete []a;
        return res;
    }
};

 

 

class Solution {
public:
    string simplifyPath(string path) {
        int n = path.size();
        auto& a = path;
        if (n == 0) return path;
        
        char *c = new char[n+1];
        memcpy(c, path.c_str(), n+1);
        
        int idx = 0;
        char *s = c, *e;
        while (*s != '\0') {
            s = getFirst(s);
            e = getLast(s);
            if (*s == '\0') break;
            if (e == s && *s == '.') s = e + 1;
            else if (e - s == 1 && *s == '.' && *e == '.') {
                if (idx > 0)
                    idx = up(c, idx-1);
                s = e+1;
            }
            else {
                c[idx++] = '/';
                while (s <= e) c[idx++] = *s++;
            }
        }
        if (idx == 0) c[idx++] = '/';
        c[idx] = '\0';
        return string(c);
    }
    
    int up(char*c, int i) {
        while (i > 0 && c[i] != '/') i--;
        return i;
    }
    char* getFirst(char *c) {
        while (*c == '/') c++;
        return c;
    }
    char* getLast(char * c) {
        while (*c != '/' && *c != '\0') c++;
        return c-1;
    }

};

 

分享到:
评论

相关推荐

    c语言-leetcode题解之0071-simplify-path.zip

    c c语言_leetcode题解之0071_simplify_path.zip

    js-leetcode题解之71-simplify-path.js

    javascript js_leetcode题解之71-simplify-path.js

    leetcode双人赛-71.-Simplify-Path-Leetcode:71.-Simplify-Path-Leetcode

    leetcode双人赛71.-Simplify-Path-Leetcode 给定一个字符串路径,它是 Unix 样式文件系统中文件或目录的绝对路径(以斜杠“/”开头),将其转换为简化的规范路径。 在 Unix 风格的文件系统中,句点 '.' 指的是当前...

    71. Simplify Path**

    https://leetcode.com/problems/simplify-path/ 题目描述 Given an absolute path for a file &#40;Unix-style&#41;, simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file ...

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    _leetcode-python.pdf

    - Simplify Path: 给定一个表示文件系统的路径,请实现一个方法,以简化这个路径。 - Edit Distance: 给定两个单词word1和word2,计算将word1转换为word2所使用的最少操作数。 - Set Matrix Zeroes: 给定一个m×n的...

    leetcode中国-leetcode:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer ...Simplify Path,字符串处理,stack Length of Last Word,字符串处理.细节题 Rever

    java-leetcode面试题解Stack之第71题简化路径-题解.zip

    LeetCode第71题的全称是“Simplify Path”,该题目要求我们编写一个程序,将输入的复杂路径字符串简化为最基础的目录序列。例如,输入的路径可能是"/a/./b/../../c/",输出应该是"/c"。这个问题主要考察的是对字符串...

    c++-c++编程基础之leetcode题解第71题简化路径.zip

    在本压缩包中,主题聚焦于C++编程基础与LeetCode算法题目的解法,特别是针对LeetCode第71题“简化路径”(Simplify Path)的探讨。这道题目旨在检验开发者的路径解析能力,涉及到字符串处理和栈的数据结构应用。让...

Global site tag (gtag.js) - Google Analytics