LeetCode 71. Simplify Path
来源:互联网 发布:淘宝男士机械手表 编辑:程序博客网 时间:2024/06/07 06:23
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
简化unix的路径符号,/..是返回上一级,/.是保持当前目录,需要做的就是字符串的判断,要有equals来判断
import java.util.Stack;public class Solution { public String simplifyPath(String path) { if(path==null||path.length()==0)return path; Stack<String> s = new Stack<String>(); String[]w = path.split("/"); for(int i=0;i<w.length;i++){ if(w[i].equals(".")||w[i].length()==0)continue; if(w[i].equals("..")){ if(!s.isEmpty())s.pop(); } else s.push(w[i]); } Stack<String>t = new Stack<String>(); while(!s.isEmpty()){ t.push("/"+s.pop()); } if(t.isEmpty()){ t.push("/"); } StringBuilder sb = new StringBuilder(); while(!t.isEmpty()){ sb.append(t.pop()); } return sb.toString(); }}
阅读全文
0 0
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- [leetcode] 71.Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- [leetcode] 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode - 71. Simplify Path
- [LeetCode] 71. Simplify Path
- leetcode 71. Simplify Path
- UE4 UMG(unity的NGUI的关联理解)
- JS补充学习
- 合唱队
- POJ 3662 Telephone Lines(Dijkstra的邻接表存储)(可以作为模板题参考)
- c/c++中const的用法
- LeetCode 71. Simplify Path
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次
- STL vector学习笔记
- Android Studio下 NDK配置与Jni开发流程
- C-C++语言结构体中定义另一个结构体指针的经验
- codeforces 559C Gerald and Giant Chess
- PHP 面向对象
- 输一个递增排序的数组和一个数字 s,在数组中查找两个数使得它们的 和正好是 s
- Bootstrap:表单和图片