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 ...
随机推荐
- Meteor错误:TypeError: Meteor.userId is not a function
问题描述: 浏览器console提示错误TypeError: Meteor.userId is not a function. 原因分析: 通过查看Meteor API文档,可知该函数由包accoun ...
- WebService开发常用功能详解
一.WebService中常用的属性(Attributes)1. Web Service(Web服务)提供以下三个属性. Namespace:此属性的值包含 XML Web Service的默认 ...
- Oracle Split Partitions
1. 创建分离分区的存储过程 CREATE OR REPLACE Procedure SP_Split_Partition( v_table_name_in in varchar2, v_part_n ...
- cocos2d-x实战 C++卷 学习笔记--第6章 场景与层
前言: 一个场景(Scene)是由多个层(Layer)组成,而且层的个数要至少是1,不能为0. 场景切换 场景切换相关函数 1)void runWithScene(Scene* scene) 该函 ...
- css3 animation实现逐帧动画
css3里面的animation属性非常强大,但是自己用的比较少,最近有次面试就刚好被问到了,趁现在有时间就对animation做一个小总结.同时实现一个逐帧动画的demo作为练习 animation ...
- [PR & ML 6] [Introduction] Information Theory
- 将博客搬迁至CSDN
CSDN不给我搬家就算了....我自己搬= = http://blog.csdn.net/ourfutr2330
- Jquery ajax使用json形式通信
前台JS $.ajax({ type: 'post', url: 'HandlerL ...
- 机器学习实战:数据预处理之独热编码(One-Hot Encoding)
问题由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑一下的三个特征: ["male", "female"] ["from ...
- 学会Twitter Bootstrap不再难
Twitter Bootstrap 3.0 是对其过去的重大改变,现在它更偏向于移动应用的框架,并且宣称是最好的web设计css框架之一,的确如此. 可能有人曾经使用过Twitter Bootstra ...