待解决 leetcode71 简化路径 栈的应用
用多种ifelse很不好很复杂容易丢情况
class Solution {
public:string simplifyPath(string path) {stack<char> st;string result;int n = path.size();while(n > 1 && (path[n-1] == '/' || path[n-1] == '.')){if(n > 2 && path[n-2] == '.' && path[n-1] == '.' ){break;}if(n > 2 && path[n-2] != '/' && path[n-1] == '.' ){break;}n -- ;}for(int i = 0; i <= n-1; i++){while(!st.empty() && st.top() == '/' && path[i] == '/'){i++;}if(!st.empty() && st.top() == '/' && path[i] == '.' && (i + 1 >= n || path[i+1] =='/')){i++;continue;}else if(!st.empty() && st.top() == '/' && path[i] == '.' && i+1 < n && path[i+1] == '.' && (i + 2 >= n || path[i+2] == '/')){i += 2;if(!st.empty()){st.pop();if(st.empty()){st.push('/');}while(!st.empty() && st.top() != '/'){st.pop();}}continue;}else{st.push(path[i]);}}if (!st.empty() && st.top() == '/' && st.size() > 1) {st.pop();}for(int i = st.size();i>0;i--){result = st.top() + result;st.pop();}return result;}
};
另一种方法等一等后面加入吧