您的位置:首页 > 编程语言 > Java开发

LeetCode_71---Simplify Path

2015-07-01 10:59 405 查看
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = 
"/home/"
, => 
"/home"

path = 
"/a/./b/../../c/"
, => 
"/c"


click to show corner cases.
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"
.

Hide Tags
 Stack String

翻译:

Code:

/**
*
*/
package From61;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.Stack;

/**
* @author MohnSnow
* @time 2015年7月1日 上午10:58:22
* @translate 给定一个Unix风格的路径,简化之。使其不改变路径的结果,但是去掉中间无用的字符。
* 因为系统执行的时候也是逐段查看的,因此最直观的做法就是使用栈来简化,
* 当是/..时,出栈;
* 当是/.时,忽视;
* 当是//a时进栈/a;
* 当是其他时才进栈。
*/
public class LeetCode71 {

/**
* @param argsmengdx
* -fnst
*/
public static String simplifyPath1(String path) {
Set<String> isSkip = new HashSet<>(Arrays.asList("", ".", ".."));
Deque<String> stack = new ArrayDeque<>();
System.out.println(Arrays.toString(path.split("/")));
for (String token : path.split("/")) {
if (token.equals("..") && !stack.isEmpty())
stack.pop();
if (isSkip.contains(token))
continue;
stack.push(token);
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append("/" + stack.pollLast());
}
return sb.length() == 0 ? "/" : sb.toString();
}

//348msAC----path.split("/")的应用是经典
public static String simplifyPath2(String path) {
Deque<String> stack = new ArrayDeque<String>();
for (String token : path.split("/")) {
if (token.equals("..") && !stack.isEmpty())
stack.pop();
if (token.equals(".") || token.equals("") || token.equals(".."))
continue;
stack.push(token);
}
System.out.println(stack.toString());
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append("/" + stack.pollLast());
}
return sb.length() == 0 ? "/" : sb.toString();
}

//https://leetcode.com/discuss/22592/java-10-lines-solution-with-stack
public static String simplifyPath3(String path) {
Deque<String> stack = new LinkedList<>();
Set<String> skip = new HashSet<>(Arrays.asList("..", ".", ""));
for (String dir : path.split("/")) {
if (dir.equals("..") && !stack.isEmpty())
stack.pop();
else if (!skip.contains(dir))
stack.push(dir);
}
String res = "";
for (String dir : stack)
res = "/" + dir + res;
return res.isEmpty() ? "/" : res;
}

public static String simplifyPath(String path) {
StringBuffer result = new StringBuffer();
int last = 0;
while (last < path.length()) {
if (last == (path.length() - 1) && path.charAt(last) == '/') {// 最后一个是/
break;
}
result.append('/');
while (last + 1 < path.length() && '/' == path.charAt(last + 1)) {// //--->/
last++;
}
if (last + 1 < path.length() && '.' == path.charAt(last + 1) && '.' == path.charAt(last)) {// /..----out
last++;
last++;
result.deleteCharAt(result.length() - 1);
while (result.charAt(result.length() - 1) != '/') {
result.deleteCharAt(result.length() - 1);
}
result.deleteCharAt(result.length() - 1);
continue;
}
if (last + 1 < path.length() && '.' == path.charAt(last + 1)) {// /.--->忽略此次情况
last++;
result.deleteCharAt(result.length() - 1);
continue;
}
while (last < path.length() && '/' != path.charAt(last)) {// /home--->加入
result.append(path.charAt(last));
last++;
}
}
return result.toString();
}

public static void main(String[] args) {
String path = "/home/a/..//a/..";
System.out.println("path: " + path);
//System.out.println("simplifyPath: " + simplifyPath(path));
System.out.println("simplifyPath1: " + simplifyPath1(path));
System.out.println("simplifyPath2: " + simplifyPath2(path));
System.out.println("simplifyPath3: " + simplifyPath2(path));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode 算法 java