Problem 71: Simplify Path
思路
public class Solution {
public String simplifyPath(String path) {
String result = "/";
String[] arr = path.split("/");
ArrayList<String> buildPath = new ArrayList<String>();
for (String s : arr) {
if (s.equals("..")) {
if (buildPath.size() > 0) {
buildPath.remove(buildPath.size() - 1);
}
} else if (!s.equals(".") && !s.equals("")) {
buildPath.add(s);
}
}
for (String str : buildPath) {
result += str + "/";
}
if (result.length() > 1) {
result = result.substring(0, result.length() - 1);
}
return result;
}
}易错点
Last updated