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 ...
随机推荐
- .bak文件在英文版的sqlserver2014如何生成和恢复
生成bak备份文件 1.选择数据库 2.右击选择task 3.选择backup 4.
- ### 学习《C++ Primer》- 8
Part 8: 面向对象(第15章) // @author: gr // @date: 2015-01-09 // @email: forgerui@gmail.com 一.OOP 面向对象程序设计的 ...
- JS基本类型和引用类型的值
JS中可以把变量分成两部分,基本类型和引用类型. 基本类型比较简单,包括:Undefined.Null.Boolean.Number和String,基本类型值就是简单的数据段:引用类型值可能由多个值构 ...
- Cabarc Overview (Microsoft TechNet)
Original Link: Cabarc Overview Applies To: Windows Server 2003, Windows Server 2003 R2, Windows Ser ...
- Cloudcraft: 云架构图形可视化(智能AWS图表)
Cloudcraft: 云架构图形可视化(智能AWS图表) 2016.09.11 官方网站: https://cloudcraft.co/ Cloudcraft是一个Web应用,用图形表示各种AWS服 ...
- jquery改变多个css样式
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax ...
- java basic
//java 声明常量 //final 数据类型 常量名=值; //as: final float PI=3.14f;/ PI=3.14002F //默认浮点为 double //break:跳出多重 ...
- 伪装隐藏Nginx,PHP版本号提升服务器安全性
可能有时候我们看某些站点想知道别人在使用什么版本的web服务器之类的信息时,却发现并未显示版本号,甚至连WEB服务器都有变化,可以通过以下 方法来隐藏Nginx.PHP的版本号信息,来提升一定的安全性 ...
- Universal-Image-Loader(UIL)使用方法&流程图&源码分析 ----- 未完
GitHub源码: Android-Universal-Image-Loader Features Multithread image loading (async or sync) 多线程加载(同步 ...
- 温故知新——json
Json简介 Json(JavaScript Object Notation)是一种轻量级的数据交换格式.它是基于javascript(Standard ECMA-262 3rd Edition - ...