LeetCode_71---Simplify Path
来源:互联网 发布:药品数据库 编辑:程序博客网 时间:2024/06/07 03:51
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
click to show corner cases.
Corner Cases:
Stack String- 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"
.
翻译:
Code:
/** * */package From61;import java.util.ArrayDeque;import java.util.Arrays;import java.util.Deque;import java.util.HashSet;import java.util.LinkedList;import java.util.Set;import java.util.Stack;/** * @author MohnSnow * @time 2015年7月1日 上午10:58:22 * @translate 给定一个Unix风格的路径,简化之。使其不改变路径的结果,但是去掉中间无用的字符。 * 因为系统执行的时候也是逐段查看的,因此最直观的做法就是使用栈来简化, * 当是/..时,出栈; * 当是/.时,忽视; * 当是//a时进栈/a; * 当是其他时才进栈。 */public class LeetCode71 {/** * @param argsmengdx * -fnst */public static String simplifyPath1(String path) {Set<String> isSkip = new HashSet<>(Arrays.asList("", ".", ".."));Deque<String> stack = new ArrayDeque<>();System.out.println(Arrays.toString(path.split("/")));for (String token : path.split("/")) {if (token.equals("..") && !stack.isEmpty())stack.pop();if (isSkip.contains(token))continue;stack.push(token);}StringBuilder sb = new StringBuilder();while (!stack.isEmpty()) {sb.append("/" + stack.pollLast());}return sb.length() == 0 ? "/" : sb.toString();}//348msAC----path.split("/")的应用是经典public static String simplifyPath2(String path) {Deque<String> stack = new ArrayDeque<String>();for (String token : path.split("/")) {if (token.equals("..") && !stack.isEmpty())stack.pop();if (token.equals(".") || token.equals("") || token.equals(".."))continue;stack.push(token);}System.out.println(stack.toString());StringBuilder sb = new StringBuilder();while (!stack.isEmpty()) {sb.append("/" + stack.pollLast());}return sb.length() == 0 ? "/" : sb.toString();}//https://leetcode.com/discuss/22592/java-10-lines-solution-with-stackpublic static String simplifyPath3(String path) {Deque<String> stack = new LinkedList<>();Set<String> skip = new HashSet<>(Arrays.asList("..", ".", ""));for (String dir : path.split("/")) {if (dir.equals("..") && !stack.isEmpty())stack.pop();else if (!skip.contains(dir))stack.push(dir);}String res = "";for (String dir : stack)res = "/" + dir + res;return res.isEmpty() ? "/" : res;}public static String simplifyPath(String path) {StringBuffer result = new StringBuffer();int last = 0;while (last < path.length()) {if (last == (path.length() - 1) && path.charAt(last) == '/') {// 最后一个是/break;}result.append('/');while (last + 1 < path.length() && '/' == path.charAt(last + 1)) {// //--->/last++;}if (last + 1 < path.length() && '.' == path.charAt(last + 1) && '.' == path.charAt(last)) {// /..----outlast++;last++;result.deleteCharAt(result.length() - 1);while (result.charAt(result.length() - 1) != '/') {result.deleteCharAt(result.length() - 1);}result.deleteCharAt(result.length() - 1);continue;}if (last + 1 < path.length() && '.' == path.charAt(last + 1)) {// /.--->忽略此次情况last++;result.deleteCharAt(result.length() - 1);continue;}while (last < path.length() && '/' != path.charAt(last)) {// /home--->加入result.append(path.charAt(last));last++;}}return result.toString();}public static void main(String[] args) {String path = "/home/a/..//a/..";System.out.println("path: " + path);//System.out.println("simplifyPath: " + simplifyPath(path));System.out.println("simplifyPath1: " + simplifyPath1(path));System.out.println("simplifyPath2: " + simplifyPath2(path));System.out.println("simplifyPath3: " + simplifyPath2(path));}}
0 0
- LeetCode_71---Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- 从此SQLPLUS有了Top命令
- Storm 配置图文解析
- AD域 技術整理
- 谁才是最快的消息队列:ActiveMQ, RabbitMQ, HornetQ, QPID
- uc/os-ii任务与任务管理
- LeetCode_71---Simplify Path
- leetcode[28]:Implement strStr()
- apt在线/离线安装CDH5.1.2
- 辛星浅析php正则匹配中的utf字符串
- MYSQL学习2数据库-表-字段
- MCU GPIO模拟spi
- centos killall安装
- iOS开发常见错误
- 使用php的socket发送tcp请求