Skip to content

Instantly share code, notes, and snippets.

@chuanying
Created September 25, 2013 03:59
Show Gist options
  • Save chuanying/6695030 to your computer and use it in GitHub Desktop.
Save chuanying/6695030 to your computer and use it in GitHub Desktop.
Simplify Path
// 1.
class Solution {
public:
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> vec;
int pos = path.find('/');
while (pos != string::npos && pos < path.length()) {
int pos2 = path.find('/', pos + 1);
string tmp;
if (pos2 == string::npos) {
tmp = path.substr(pos + 1);
} else {
tmp = path.substr(pos + 1, pos2 - pos - 1);
}
if (tmp == "..") {
if (!vec.empty()) {
vec.pop_back();
}
} else if (tmp != "." && tmp.length() > 0) {
vec.push_back(tmp);
}
pos = pos2;
}
string ans;
for (int i = 0; i < vec.size(); ++i) {
ans += "/" + vec[i];
}
return ans.length() ? ans : "/";
}
};
// 2.
class Solution {
public:
string simplifyPath(string path) {
vector<string> vec;
int b = 0; //pointer to the first non '/'
int e = 0; //pointer to the first '/' or the end,
//path.substr(b, e - b) is a valid segment
while (b < path.length()) {
while (b < path.length() && path[b] == '/') {
++b;
}
e = b + 1;
while (e < path.length() && path[e] != '/') {
++e;
}
string t = path.substr(b, e - b);
if (t == "..") {
if (!vec.empty()) {
vec.pop_back();
}
} else if (t.length() && t != ".") {
vec.push_back(t);
}
b = e + 1;
}
string ans;
for (int i = 0; i < vec.size(); ++i) {
ans += "/" + vec[i];
}
return ans.length() ? ans : "/";
}
};
// 3.
string simplifyPath(string path) {
vector<string> s;
int pos1, pos2;
pos1 = pos2 = 0;
while (1) {
pos2 = path.find('/', pos1);
string ss = path.substr(pos1, pos2 - pos1);
if (ss == ".." && !s.empty() ) {
s.pop_back();
} else if(ss.length() && ss != ".." && ss != "." && ss != "/") {
s.push_back(ss);
}
if (pos2 == string::npos) {
break;
}
pos1 = pos2 + 1;
}
string ans;
for (int i = 0; i < s.size(); ++i) {
ans += "/" + s[i];
}
return ans.length() ? ans : "/";
}
// 4.
string simplifyPath(string path) {
vector<string> ss;
int i, last;
for (i = 0, last = 0; i <= path.length(); ++i) {
if (i == path.length() || path[i] == '/') {
if (i > last) {
string t = path.substr(last, i - last);
if (!ss.empty() && t == "..") {
ss.pop_back();
} else if (t != "." && t != "..") {
ss.push_back(t);
}
}
last = i+1;
}
}
string ans = "/";
for (int i = 0; i < ss.size(); ++i ) {
ans += ss[i] + "/";
}
return ans.length() == 1 ? ans : ans.substr(0, ans.length() - 1);
}
// 5.
string simplifyPath(string path) {
int start_pos = path.find_first_not_of('/');
if (start_pos == string::npos) {
return "/";
}
string ret;
for (int i = start_pos + 1; i <= path.length(); i++) {
if (path[i] == '/' || i == path.length()) {
string sub = path.substr(start_pos, i - start_pos);
if (sub == "..") {
int rpos = ret.rfind('/');
if (string::npos != rpos) {
ret = ret.substr(0, rpos);
}
} else if (sub != ".") {
ret += string(1, '/') + sub;
}
start_pos = path.find_first_not_of('/', i);
if (start_pos == string::npos) {
break;
}
i = start_pos;
}
}
return ret.size() ? ret : "/";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment