Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
86 lines (71 loc) · 1.92 KB

File metadata and controls

86 lines (71 loc) · 1.92 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Source : https://oj.leetcode.com/problems/simplify-path/
// Author : Hao Chen
// Date : 2014-10-09
/**********************************************************************************
*
* 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".
*
*
**********************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
string simplifyPath(string path) {
string result;
vector<string> elems = split(path, '/');
int ignor = 0;
for(int i=elems.size()-1; i>=0; i--) {
if (elems[i]=="" || elems[i]=="." ){
continue;
}
if (elems[i]==".."){
ignor++;
continue;
}
if (ignor>0){
ignor--;
continue;
}
if (result.size()==0){
result = "/" + elems[i];
}else{
result = "/" + elems[i] + result;
}
}
return result.size() ? result : "/";
}
int main(int argc, char** argv)
{
string path("/a/./b/../../c/");
if (argc > 1 ){
path = argv[1];
}
cout << path << " : " << simplifyPath(path) << endl;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.