-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |