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; } };
相关推荐
主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
LeetCode — Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need...
leetcode双人赛71.-Simplify-Path-Leetcode 给定一个字符串路径,它是 Unix 样式文件系统中文件或目录的绝对路径(以斜杠“/”开头),将其转换为简化的规范路径。 在 Unix 风格的文件系统中,句点 '.' 指的是当前...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
https://leetcode.com/problems/simplify-path/ 题目描述 Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file ...
大佬的leetcode刷题笔记(c++版本)
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
leetcode中文版
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
leetcode 答案Leetcode-emacs 这是一个 Emacs 插件,可以让编写 leetcode 程序变得更容易和更快。 先决条件 leetcode-cli 该工具基于 ...leetcode-path : 指定保存解决方案代码的路径 leetcode-language : 分
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
leetcode高频面试笔试题150+道,亲身总结。
LeetCode面试笔试题