C++實現(xiàn)LeetCode(71.簡化路徑)
[LeetCode] 71.Simplify Path 簡化路徑
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
- 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".
這道題讓簡化給定的路徑,光根據(jù)題目中給的那一個例子還真不太好總結(jié)出規(guī)律,應(yīng)該再加上兩個例子 path = "/a/./b/../c/", => "/a/c"和path = "/a/./b/c/", => "/a/b/c", 這樣我們就可以知道中間是"."的情況直接去掉,是".."時刪掉它上面挨著的一個路徑,而下面的邊界條件給的一些情況中可以得知,如果是空的話返回"/",如果有多個"/"只保留一個。那么我們可以把路徑看做是由一個或多個"/"分割開的眾多子字符串,把它們分別提取出來一一處理即可,代碼如下:
C++ 解法一:
class Solution {
public:
string simplifyPath(string path) {
vector<string> v;
int i = 0;
while (i < path.size()) {
while (path[i] == '/' && i < path.size()) ++i;
if (i == path.size()) break;
int start = i;
while (path[i] != '/' && i < path.size()) ++i;
int end = i - 1;
string s = path.substr(start, end - start + 1);
if (s == "..") {
if (!v.empty()) v.pop_back();
} else if (s != ".") {
v.push_back(s);
}
}
if (v.empty()) return "/";
string res;
for (int i = 0; i < v.size(); ++i) {
res += '/' + v[i];
}
return res;
}
};
還有一種解法是利用了C語言中的函數(shù)strtok來分隔字符串,但是需要把string和char*類型相互轉(zhuǎn)換,轉(zhuǎn)換方法請猛戳這里。除了這塊不同,其余的思想和上面那種解法相同,代碼如下:
C 解法一:
class Solution {
public:
string simplifyPath(string path) {
vector<string> v;
char *cstr = new char[path.length() + 1];
strcpy(cstr, path.c_str());
char *pch = strtok(cstr, "/");
while (pch != NULL) {
string p = string(pch);
if (p == "..") {
if (!v.empty()) v.pop_back();
} else if (p != ".") {
v.push_back(p);
}
pch = strtok(NULL, "/");
}
if (v.empty()) return "/";
string res;
for (int i = 0; i < v.size(); ++i) {
res += '/' + v[i];
}
return res;
}
};
C++中也有專門處理字符串的機(jī)制,我們可以使用stringstream來分隔字符串,然后對每一段分別處理,思路和上面的方法相似,參見代碼如下:
C++ 解法二:
class Solution {
public:
string simplifyPath(string path) {
string res, t;
stringstream ss(path);
vector<string> v;
while (getline(ss, t, '/')) {
if (t == "" || t == ".") continue;
if (t == ".." && !v.empty()) v.pop_back();
else if (t != "..") v.push_back(t);
}
for (string s : v) res += "/" + s;
return res.empty() ? "/" : res;
}
};
Java 解法二:
public class Solution {
public String simplifyPath(String path) {
Stack<String> s = new Stack<>();
String[] p = path.split("/");
for (String t : p) {
if (!s.isEmpty() && t.equals("..")) {
s.pop();
} else if (!t.equals(".") && !t.equals("") && !t.equals("..")) {
s.push(t);
}
}
List<String> list = new ArrayList(s);
return "/" + String.join("/", list);
}
}
到此這篇關(guān)于C++實現(xiàn)LeetCode(71.簡化路徑)的文章就介紹到這了,更多相關(guān)C++實現(xiàn)簡化路徑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vscode+wsl運(yùn)行編譯c++的實現(xiàn)
本文主要介紹了vscode+wsl運(yùn)行編譯c++的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
淺析C++中strlen函數(shù)的使用與模擬實現(xiàn)strlen的方法
這篇文章主要介紹了strlen函數(shù)的使用與模擬實現(xiàn)strlen的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
深入探索C++中stack和queue的底層實現(xiàn)
這篇文章主要介紹了C++中的stack和dequeue的底層實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
C++深入探索內(nèi)聯(lián)函數(shù)inline與auto關(guān)鍵字的使用
本篇文章主要包括內(nèi)聯(lián)函數(shù)和auto關(guān)鍵字。其中,內(nèi)斂函數(shù)包括概念,特性等;auto關(guān)鍵字的使用規(guī)則,使用場景等,接下來讓我們深入了解2022-05-05
C++20 特性 協(xié)程 Coroutines(1)
這篇文章主要給大家分享得是C++20 得特性 協(xié)程 Coroutines,下面文章內(nèi)容我們將來具體介紹什么是協(xié)程,協(xié)程得好處等知識點,需要的朋友可以參考一下2021-10-10
C++友元函數(shù)與拷貝構(gòu)造函數(shù)詳解
這篇文章主要介紹了C++友元函數(shù)與拷貝構(gòu)造函數(shù),需要的朋友可以參考下2014-07-07

