【Leetcode】 Simplify Path
来源:互联网 发布:免费联系人恢复软件 编辑:程序博客网 时间:2024/06/06 14:48
题目链接:https://leetcode.com/problems/simplify-path/
题目:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
思路:
easy
算法:
public String simplifyPath(String path) { String s[] = path.trim().split("\\/+"); Stack<String> stack = new Stack<String>(); for (String str : s) { if (!str.equals("") && !str.equals(" ") && !str.equals(".") && !str.equals("..")) { stack.push(str); } else if (str.equals("..") && stack.size() != 0) { stack.pop(); } } String res = "";// 将栈中剩余元素组合成路径 for (int i = 0; i < stack.size(); i++) { res = "/" + stack.get(stack.size() - 1 - i) + res; } if (res.equals("")) // 防止/../情况 res = "/"; return res; }
1 0
- LeetCode: Simplify Path
- LeetCode Simplify Path
- LeetCode : Simplify Path
- [Leetcode] Simplify Path
- [leetcode] simplify path
- [LeetCode] Simplify Path
- leetcode 100: Simplify Path
- [leetcode]Simplify Path
- [LeetCode]Simplify Path
- [leetCode] Simplify Path
- [leetcode]Simplify Path
- [leetcode] Simplify Path
- [LeetCode] Simplify Path
- leetcode simplify path
- Leetcode: Simplify Path
- LeetCode题解:Simplify Path
- [LeetCode] Simplify Path
- LeetCode Simplify Path
- 资料收藏
- Java学习笔记之String、StringBuffer和StringBuilder
- 操作系统常见面试题总结
- 【Leetcode】Binary Tree Zigzag Level Order Traversal
- java设计模式1 单例模式
- 【Leetcode】 Simplify Path
- 子类对象实例化全过程
- 使用AVFoundation扫描二维码
- MySQL 5.7.10 自动备份、自动清理旧备份集
- Sflow介绍
- java future 和 callable的一个例子
- SHELL NOTES
- 《Thinking in Java》读书笔记之内部类
- RecycleView使用问题梳理