本文主要是介绍C++ | Leetcode C++题解之第71题简化路径,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {
public:string simplifyPath(string path) {auto split = [](const string& s, char delim) -> vector<string> {vector<string> ans;string cur;for (char ch: s) {if (ch == delim) {ans.push_back(move(cur));cur.clear();}else {cur += ch;}}ans.push_back(move(cur));return ans;};vector<string> names = split(path, '/');vector<string> stack;for (string& name: names) {if (name == "..") {if (!stack.empty()) {stack.pop_back();}}else if (!name.empty() && name != ".") {stack.push_back(move(name));}}string ans;if (stack.empty()) {ans = "/";}else {for (string& name: stack) {ans += "/" + move(name);}}return ans;}
};
这篇关于C++ | Leetcode C++题解之第71题简化路径的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!