Leetcode 71. Simplify Path
来源:互联网 发布:崩坏3rd淘宝代充 编辑:程序博客网 时间:2024/05/18 01:55
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) { if (path[0] != '/') return string(); size_t n = path.size(); string ret; size_t i = 0; while (i < n) { if (path[i] == '/') { ++i; while (i != n && path[i] == '/') ++i; if (i == n) break; string tmp; while (i != n && path[i] != '/') { tmp += path[i]; ++i; } if (tmp == ".."){ int j = ret.size() - 1; while(ret[j] != '/') --j; if (j < 0) j = 0; ret.erase(ret.begin() + j, ret.end()); } else if (tmp != ".") ret += ('/' + tmp); } } if (ret.size() == 0) return string(1, '/'); if (ret[0] = '/') return ret; else return '/' + ret; }};
参考后
class Solution {public: string simplifyPath(string path) { stringstream ss(path); string tmp; string ret; vector<string> v; while(getline(ss, tmp, '/')) { if (tmp == "" || tmp == ".") continue; if (tmp == "..") { if (!v.empty()) v.pop_back(); continue; } v.push_back(tmp); } if (v.empty()) return string(1, '/'); for (auto str : v) { ret += ('/' + str); } return ret; }};
阅读全文
0 0
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- [leetcode] 71.Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- [leetcode] 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode - 71. Simplify Path
- [LeetCode] 71. Simplify Path
- leetcode 71. Simplify Path
- jdbc&sqlite
- nginx缩略图image_filter配置整合fastdfs
- 13周
- Java小练习——简单的登录界面
- 趣图:改 Bug
- Leetcode 71. Simplify Path
- 面向对象:在平淡的生活中,活成不平凡的你我,希望你是那个对的他
- 主题:免费的论文查重网站
- AJAX简介
- 第一位做游戏开发与设计的女程序员
- ios-锁和空间和单例知识点
- MatConvNet的安装和使用说明
- Java构造方法、重载、访问修饰符
- HTML常用标签列表