LeetCode Simplify Path
2015-06-30 00:25
267 查看
Description:
Given an absolute path for a file (Unix-style), simplify it.
Solution:
用scanner和stack处理即可,用"/"作为分割器,然后每次遇到".."就出栈。最后将stack倒序输出即可。
Given an absolute path for a file (Unix-style), simplify it.
Solution:
用scanner和stack处理即可,用"/"作为分割器,然后每次遇到".."就出栈。最后将stack倒序输出即可。
import java.util.*; public class Solution { public String simplifyPath(String path) { Scanner scan = new Scanner(path); scan.useDelimiter("/"); String temp; Stack<String> stack = new Stack<>(); while (scan.hasNext()) { temp = scan.next(); if (temp.equals("..")) { if (!stack.isEmpty()) stack.pop(); } else if (temp.equals(".")) { } else if (temp.equals("")) { } else { stack.add(temp); } } String ans = ""; while (!stack.isEmpty()) { temp = stack.pop(); if (temp.equals(" ")) break; ans = "/" + temp + ans; } if (ans.equals("")) ans = "/"; // System.out.println(ans); return ans; } }
相关文章推荐
- 2015年6月29日 课设日志
- win7-时间更新
- 2015062910 - 一周搞定Effective java
- android searchView加ListView共同使用
- mysql定时器
- 定义一个功能,记录程序运行的次数。满足5次后,给出提示,试用次数已到,请注册
- 输入框只能输入数字 以及其他限制的总结
- AFNetworking获取服务端plist文件,XML文件
- POJ 3592 强连通缩点+spfa最长路
- 公安项目项目感想
- 注解开发spring-aop 入门
- 2015062909 - 黑鹰坠落
- .Net 自定义事件
- .Net 自定义事件
- 第八十八天 how can I 坚持 angelaJs
- 三范式
- 37条常用Shell命令
- virtualtree 的使用(Delphi)
- 2015062908 - EffactiveJava笔记 - 第46条 foreach优先传统for循环(4)
- delphi中VirtualStringTree树使用方法之终结篇!