leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path
1 题目
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".
接口:public String simplifyPath(String path)
2 思路
简化linux路径,主要对".." 进行回退操作。直观的想到用栈,在java里,栈用Deque的实现类LinkedList来做。
- 对
path进行split - 往
stack中添加路径,进栈,出栈 - 最后将
stack中元素输出
3 代码
public String simplifyPath(String path) {
String[] words = path.split("/");
List<String> stack = new LinkedList<String>();
for (String s : words) {
if (s.equalsIgnoreCase("..")) {
if (!stack.isEmpty())
stack.remove(stack.size() - 1);
} else if (!(s.length() == 0 || s.equalsIgnoreCase("."))) {
stack.add(s);
}
}
String res = "";
for (String s : stack) {
res += "/" + s;
}
return res.length() == 0 ? "/" : res;
}
4 总结
栈的思想解决。
leetcode面试准备:Simplify Path的更多相关文章
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- 【一天一道LeetCode】#71. Simplify Path
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- leetcode面试准备:Triangle
leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
随机推荐
- xmlDoc.SelectNodes用法(获取不到节点时注意事项)
注:以下举例仅针对xml自定义了命名空间的情况,如果是其他情况,请参照他人博客~ using System;using System.Collections.Generic;using System. ...
- ios面试题集锦(一)
一.前言部分 文中的问题多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.iOS9有哪些新特性? 答案: 1)改进了 Siri 基于日期.位置和相簿名称来搜索个人照片和视频 要求 Siri 来 ...
- 【转】querystring传递中文出现乱码的问题
原帖地址:http://www.cnblogs.com/Fly-sky/archive/2009/04/22/1441015.html 现象:近期项目中用到查询字符串传值,如果传递的是英文一切正常,但 ...
- 在Mac OS X中使用VIM开发STM32(3)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在上一篇文章中,我们安装了ctags插件,ctags能对我们的源代码文件中的元素建立索引表, ...
- 小shell函数
whoport() { port=$1 echo "------ who occupied port: $port ----------" info=$(sudo lsof ...
- validate中remote的用法
jquery中的插件validate主要可以用于表单验证,极大地方便了我们,而validate中的remote方法更是非常的方便.以下介绍它的两个主要用途 1.注册时用于验证用户名是否存在 >& ...
- 常见CSS注意问题
1. 初始化css 有哪些 因 为浏览器的品种很多,每个浏览器的默认样式也是不同的,比如<button>标签,在IE浏览器.Firefox浏览器以及Safari浏览 器中的样式都是不同的, ...
- switch...case 和 if...else
switch...case与if...else的根本区别在于: switch...case会生成一个跳转表来指示实际的case分支的地址,而这个跳转表的索引号与switch变量的值是相等的,switc ...
- Python设计模式——状体模式
需求,根据当前的时间,返回工作状态 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' def get_state(hour): if hour> ...
- WPF从入门到放弃系列第二章 XAML
本文是作者学习WPF从入门到放弃过程中的一些总结,主要内容都是对学习过程中拜读的文章的整理归纳. 参考资料 XAML 概述 (WPF):https://msdn.microsoft.com/zh-cn ...