package com.wsq.stack;
import java.util.Deque;
import java.util.LinkedList;
public class SimplifyPath {
public String simplifyPath(String path) {
String[] sArr = path.split("/");
Deque<String> stack = new LinkedList();
for(String s: sArr) {
if(s.equals("..")){
if(!stack.isEmpty()) {
stack.pop();
}
}else if(!s.equals("") && !".".equals(s)) {
stack.push(s);
}
}
if(stack.isEmpty()) {
return "/";
}
String ans = "";
while(!stack.isEmpty()) {
ans = "/" + stack.pop() + ans;
}
return ans;
}
public static void main(String[] args) {
String path = "/..";
SimplifyPath sp = new SimplifyPath();
String ans = sp.simplifyPath(path);
System.out.println(ans);
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-7591.html