71. Simplify Path

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".

java的split函数,只要separator的前“或者”后没有“别的”字符串就会返回一个空。

linux系统当中,多余两个的点会被当作普通路径处理。

public class Solution {
public String simplifyPath(String path) {
if (path.length() == 0) {
return "";
}
String[] strs = path.split("/");
StringBuilder result = new StringBuilder();
LinkedList<String> stack = new LinkedList<String>();
for (String string : strs) {
switch (string) {
case "": stack.offer("/"); break;
case ".": break;
case "..": while (!stack.isEmpty() && stack.removeLast().equals("/")); break;
default: stack.offer(string);
}
}
if (path.charAt(0) == '/') {
result.append('/');
}
while (!stack.isEmpty()) {
String string = stack.removeFirst();
if (!string.equals("/")) {
result.append(string);
result.append('/');
}
}
int ll = result.length();
if (ll > 1 && result.charAt(ll - 1) == '/') {
result.deleteCharAt(ll - 1);
}
return result.toString();
}
}

[leetcode] 题型整理之字符串处理的更多相关文章

  1. [leetcode] 题型整理之二叉树

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

  2. [leetcode] 题型整理之动态规划

    动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...

  3. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  4. [leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余

    需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two ...

  5. [leetcode] 题型整理之cycle

    找到环的起点. 一快一慢相遇初,从头再走再相逢.

  6. [leetcode]题型整理之用bit统计个数

    137. Single Number II Given an array of integers, every element appears three times except for one. ...

  7. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  8. [leetcode] 题型整理之查找

    1. 普通的二分法查找查找等于target的数字 2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字 由于新的查找结果总是比旧的查找结果更接近于target,因此只 ...

  9. [leetcode] 题型整理之排序

    75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...

随机推荐

  1. Codeforces乱刷集合

    发这篇博客的目的是因为刷了些水题,但又有一些不错的地方可以加以借鉴....然后又不想一个一个发.... Codeforces731A 题目大意:给出一个26个字母的环,初始指向a,可以顺时针转或者逆时 ...

  2. Linux下因为系统编码问题造成乱码的解决办法

    2016年12月13日18:34:32 -------------------------------- 最近一段时间遇到一些润乾报表的应用在linux系统下面乱码的问题,最后检查后都发现是客户的li ...

  3. Theano: CNMeM is disabled, CuDNN not available

    Problem Theano: CNMeM is disabled, CuDNN not available Solution cnmem package: https://github.com/NV ...

  4. 个人学习记录1:二维数组保存到cookie后再读取

    二维数组保存到cookie后再读取 var heartsArray = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0],[0,0, ...

  5. 问你觉得iOS7为什么要扁平化,扁平化和之前的比有什么优势

    问你觉得iOS7为什么要扁平化,扁平化和之前的比有什么优势 苹果首席设计师谈为何会在iOS上选择扁平风格http://ndnews.oeeee.com/html/201306/11/71078.htm ...

  6. Angular.js通过bootstrap实现经典的表单提交

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel= ...

  7. Java数据结构——图

    点 //类名:Vertex //属性: //方法: class Vertex{ public char label; //点的名称,如A public boolean wasVisited; publ ...

  8. SDK 支付

    充值:用什么买什么 MSDK: Q点:百科 http://baike.baidu.com/link?url=Dw8ySUIvv6AAprULG_wnI7Mst61gG4bO2qzfpfi1j9xx6c ...

  9. golang的ssh包

    git clone https://github.com/golang/crypto.git,复制到 golang.org/x/ 目录下. 常常用来建立ssh连接发送一条命令,但有时需要模拟ssh客户 ...

  10. 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ItemSelector重构完结版)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得上一篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...