由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一道leetcode的online judge题
相关主题
a2z(amazon 子公司)电面题目请问这两个 java 语句有什么区别?
leetcode的Text Justification的OJ请教一道题:Remove adjacent duplicate char recursively fro
G题,F题,leetcode题一道面试题(integer to binary string)
关于string的substr的问题问个java hashcode的题
leetcode: simplify path贡献今天facebook电面 一道题
这段LIS为啥崩溃?问一个facebook的电面
从Simplify Path问面试编程语言选择?Text Justification
一道电面题,分享下, 这个题应该用哪几个data structure?问一道interview street 上的题
相关话题的讨论汇总
话题: string话题: home话题: bar话题: path话题: args
进入JobHunting版参与讨论
1 (共1页)
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
6
同问
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
8
能说说思路吗?
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)

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一道interview street 上的题leetcode: simplify path
G电面一题这段LIS为啥崩溃?
请教leetcode上的count and say从Simplify Path问面试编程语言选择?
好不容易写了个bug free, 可是被说会秒据, 帮看看一道电面题,分享下, 这个题应该用哪几个data structure?
a2z(amazon 子公司)电面题目请问这两个 java 语句有什么区别?
leetcode的Text Justification的OJ请教一道题:Remove adjacent duplicate char recursively fro
G题,F题,leetcode题一道面试题(integer to binary string)
关于string的substr的问题问个java hashcode的题
相关话题的讨论汇总
话题: string话题: home话题: bar话题: path话题: args