b***d 发帖数: 87 | 1 请教一道leetcode的online judge题,题目一直没看懂。
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
另外还有几个列子:
/home/foo/../bar" -> "/home/bar"
"/home/foo/./.././bar" -> "/home/bar"
"/home/of/foo/../../bar/../../is/./here/." -> "/is/here" |
x*******1 发帖数: 28835 | 2 stack.push all strings between // or / end of string
if . don;t push
if .. pop stack top
after all elements are in the stack, pop all elements, concat string from
right to left s = /element + s; |
b***d 发帖数: 87 | 3 谢谢楼上的,只是我还没读懂题意,这个simple路径到底是怎么个回事?
【在 x*******1 的大作中提到】 : stack.push all strings between // or / end of string : if . don;t push : if .. pop stack top : after all elements are in the stack, pop all elements, concat string from : right to left s = /element + s;
|
x*******1 发帖数: 28835 | 4 remove all unnecessary character like . .. keep the path as simple as /
string/string/string style |
b***d 发帖数: 87 | 5 谢谢。但下面两个例子感觉也不完全符合你给的定义啊,还有其他的规则吗?
"/home/foo/./.././bar" -> "/home/bar"
"/home/of/foo/../../bar/../../is/./here/." -> "/is/here"
【在 x*******1 的大作中提到】 : remove all unnecessary character like . .. keep the path as simple as / : string/string/string style
|
f*********m 发帖数: 726 | |
j**w 发帖数: 382 | 7 import java.util.LinkedList;
class Ans
{
public static void main(String[] args)
{
System.out.println(args[0]);
String[] dirs = args[0].split("/");
LinkedList path = new LinkedList();
for (String s : dirs)
{
if ( s.equals("") )
{
continue;
}
else if ( s.equals(".") )
{
continue;
}
else if ( s.equals(".."))
{
path.pollLast();
}
else
{
path.add(s);
}
}
StringBuffer sb = new StringBuffer();
for (String p : path )
{
sb.append("/").append(p);
}
System.out.println(sb.toString());
}
} |
f*********m 发帖数: 726 | |
y***1 发帖数: 450 | 9 很符合嚒。你应该照着他的算法做一边。
【在 b***d 的大作中提到】 : 谢谢。但下面两个例子感觉也不完全符合你给的定义啊,还有其他的规则吗? : "/home/foo/./.././bar" -> "/home/bar" : "/home/of/foo/../../bar/../../is/./here/." -> "/is/here"
|
n*******w 发帖数: 687 | 10 就是这个。类似栈。空串跟一个点可以合并判断。
unix里边,.是本目录, ..是上一级目录。
【在 j**w 的大作中提到】 : import java.util.LinkedList; : class Ans : { : public static void main(String[] args) : { : System.out.println(args[0]); : String[] dirs = args[0].split("/"); : LinkedList path = new LinkedList(); : : for (String s : dirs)
|