由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - G题,F题,leetcode题
相关主题
从Simplify Path问面试编程语言选择?Salesforce refer
关于string的substr的问题一些杂七杂八的经验
请教一道leetcode的online judge题说马工这个工作是青春饭,确实不假
leetcode: simplify path一些面经
leetcode里最弄不明白的两道题job opportunities
请教个设计题reverse random pointers of a single linked list
C++面试题目分享(2)Bay area company Hiring SQA engineer
Job Opening: Windows Software Engineeranother MIT dp problem without answer on their website.
相关话题的讨论汇总
话题: string话题: index话题: leetcode话题: stack话题: int
进入JobHunting版参与讨论
1 (共1页)
s*****i
发帖数: 32
1
Unix file path化简,写code
例如 /a/./b/../../c/ ,化简为 /c/
用stack或者d-queue,有些细节需要考虑,例如 /..//.. 还是输出 /
谁能贴一个elegant的版本。我自己的版本就是根据leetcode的case反复调整来的。真
的好烦。
感觉这道题如果test normal case的话,比较简单,test那些corner case,变得异常
讨厌,难道是在考各种corner case是否能记下来?
在这儿看大家有没有容易记的版本。谢谢!
T******e
发帖数: 157
2
class Solution {
public:
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int N = path.size();
vector folder;
int index = 0;
while (index < N){
while (index < N && path[index] == '/') index++;
if (index == N) break;
int begin = index;
while (index < N && path[index] != '/') index++;
string str = path.substr(begin,index-begin);
if (str == ".."){
if (!folder.empty()) folder.pop_back();
}
else if (str != ".") folder.push_back(str);
}
if (folder.empty()) return "/";
string simplified = "";
for (int i = 0; i < folder.size(); i++) simplified += '/' + folder[i
];
return simplified;
}
};
l*********d
发帖数: 78
3
抛砖引玉,献丑了。。
public String simplifyPath(String path) {
String[] parts = path.split("/");
Stack stack = new Stack();
for (String s : parts) {
if (s.equals("")) continue;
else if (s.equals(".")) continue;
else if (s.equals("..")) {
if (!stack.isEmpty()) stack.pop();
}
else stack.push(s);
}
StringBuilder sb = new StringBuilder();
sb.append("/");
for (int i = 0; i < stack.size(); i++) {
if (i != 0) sb.append("/");
sb.append(stack.get(i));
}
return sb.toString();
}
s*****i
发帖数: 32
4
非常感谢楼上两位!
1 (共1页)
进入JobHunting版参与讨论
相关主题
another MIT dp problem without answer on their website.leetcode里最弄不明白的两道题
Open Position (Bay Area, Java)请教个设计题
FACEBOOK的那个Chinese Specialist就是翻译吗?多少薪水?C++面试题目分享(2)
微软这次主要砍的是manager,不是码农Job Opening: Windows Software Engineer
从Simplify Path问面试编程语言选择?Salesforce refer
关于string的substr的问题一些杂七杂八的经验
请教一道leetcode的online judge题说马工这个工作是青春饭,确实不假
leetcode: simplify path一些面经
相关话题的讨论汇总
话题: string话题: index话题: leetcode话题: stack话题: int