-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedium_Prob222.cpp
More file actions
52 lines (42 loc) · 1.38 KB
/
Medium_Prob222.cpp
File metadata and controls
52 lines (42 loc) · 1.38 KB
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
/* Given a list of numbers, create an algorithm that arranges them in order to form the largest possible integer.
For example, given [10, 7, 76, 415], you should return 77641510.*/
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
/* Given an absolute pathname that may have . or .. as part of it, return the shortest standardized path.
For example, given "/usr/bin/../bin/./scripts/../", return "/usr/bin/". */
string getShortestStandardizePath(string path) {
vector<string> pathPart ;
int partBegin = 0;
for (int i=0; i<path.size(); i++) {
if (path[i] == '/') {
if (i != partBegin) {
pathPart.push_back(path.substr(partBegin,i-partBegin));
}
partBegin = i+1;
}
}
vector<string> absolutePath;
for (const string &part : pathPart) {
if (part == "..") {
if (!absolutePath.empty()) {
absolutePath.pop_back();
}
} else if (part != ".") {
absolutePath.push_back(part);
}
}
string standardizedPath;
for (const string &part : absolutePath) {
standardizedPath += "/" + part;
}
if (standardizedPath.empty()) {
return "/";
}
return standardizedPath;
}
int main(int argc, char *argv[])
{
cout << getShortestStandardizePath("/usr/bin/../bin/./scripts/../") << endl;
}