国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

53:Simplify Path

2019-11-06 08:50:58
字體:
來源:轉載
供稿:網友

題目:Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” 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”.

下面解法代碼的思想及編寫參考了網址https://github.com/soulmachine/leetcode#leetcode題解題目

代碼如下:

/ 時間復雜度 O(n),空間復雜度 O(n)class Solution {public: string simplifyPath(const string& path) { vector<string> dirs; // 當做棧 for (auto i = path.begin(); i != path.end(); ) { ++i; auto j = find(i, path.end(), '/'); auto dir = string(i, j); if (!dir.empty() && dir != '.') { // 當有連續 '///' 時,dir 為空 if (dir == '..') { if (!dirs.empty()) dirs.pop_back(); } else dirs.push_back(dir); i = j; } ostringstream out; if (dirs.empty()) out << "/"; else { for (auto dir : dirs) out << "/" << dir; } return out.str(); }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌审旗| 广州市| 扎囊县| 荆门市| 乐东| 永靖县| 华宁县| 尚义县| 呈贡县| 行唐县| 银川市| 商南县| 蕲春县| 鄂温| 武穴市| 海伦市| 藁城市| 钟山县| 文登市| 来凤县| 眉山市| 湾仔区| 梁河县| 开封县| 且末县| 明光市| 嘉峪关市| 赣州市| 田阳县| 三穗县| 融水| 靖州| 毕节市| 楚雄市| 深水埗区| 凭祥市| 都兰县| 肇东市| 湟源县| 平潭县| 精河县|