Skip to content

Commit

Permalink
SimplifyPath
Browse files Browse the repository at this point in the history
Change-Id: I2cefaca9f3b954bf64b5a3262c4b52c8b8039f32
  • Loading branch information
applewjg committed Jan 18, 2015
1 parent a5e77a5 commit d514441
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions SimplifyPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Author: Andy, [email protected]
Date: Jan 11, 2015
Problem: Simplify Path
Difficulty: Easy
Source: https://oj.leetcode.com/problems/simplify-path/
Notes:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
Solution: Add an additional '/' at the end of 'path' for simply detecting the end.
*/
public class Solution {
public String simplifyPath(String path) {
if(path.length()==0) return "/";
if(path.charAt(0)!='/') return "/";
ArrayList<String> dirs = new ArrayList<String>();
String[] str = path.split("/");
for (int i = 0; i < str.length; ++i) {
if ((i == 0 || i == str.length - 1) && str[i].compareTo("") == 0) continue;
if (str[i].compareTo("..") == 0) {
if (dirs.isEmpty() == false) {
dirs.remove(dirs.size() - 1);
}
} else if ((str[i].compareTo(".") != 0) && (str[i].compareTo("") != 0)) {
dirs.add(str[i]);
}
}
if (dirs.isEmpty() == true) return "/";
StringBuilder res = new StringBuilder();
for (int i = 0; i < dirs.size(); ++i) {
res.append("/");
res.append(dirs.get(i));
}
return res.toString();
}
}

0 comments on commit d514441

Please sign in to comment.