[leetcode] 题型整理之字符串处理
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] 题型整理之字符串处理的更多相关文章
- [leetcode] 题型整理之二叉树
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...
- [leetcode] 题型整理之动态规划
动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- [leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余
需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two ...
- [leetcode] 题型整理之cycle
找到环的起点. 一快一慢相遇初,从头再走再相逢.
- [leetcode]题型整理之用bit统计个数
137. Single Number II Given an array of integers, every element appears three times except for one. ...
- [leetcode] 题型整理之图论
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...
- [leetcode] 题型整理之查找
1. 普通的二分法查找查找等于target的数字 2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字 由于新的查找结果总是比旧的查找结果更接近于target,因此只 ...
- [leetcode] 题型整理之排序
75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...
随机推荐
- Mac下Intellij IDEA Console中文是?
Mac下Intellij IDEA Console中文是?,解决办法是在运行时的VM参数配置也加入: -Dfile.encoding=UTF-8
- Alpha阶段第八次Scrum Meeting
情况简述 Alpha阶段第八次Scrum Meeting 敏捷开发起始时间 2016/10/31 00:00 敏捷开发终止时间 2016/10/32 00:00 会议基本内容摘要 跟助教进行了交流,明 ...
- 正则去掉img标签的style样式
$body = '<div style="width:100px; height:20px;"><img alt="test" src=&qu ...
- 11月15日下午 ajax返回数据类型为XML数据的处理
ajax返回数据类型为XML数据的处理 /*XML:可扩展标记语言 HTML:超文本标记语言 标签:<标签名></标签名> 特点: 1.必须要有一个根 2.标签名自定义 3.对 ...
- 如何合并两个Docker 镜像
http://www.open-open.com/lib/view/open1437746544709.html 在你的机器上使用docker pull来从Docker Hub下载镜像. docker ...
- 5Hibernate配置及使用方法----青软S2SH(笔记)
关于hibernate的简单配置,先看结构图,我们需要 1.还要弄一下需要的 jar包. 2.配置两个文件(hibernate配置文件和映射文件),不过映射文件可以用注解替代. 3.写一个pojo类, ...
- Excel函数——DATE、SUBSTITUTE、REPLACE、ISERROR、IFERROR
1.DATE DATE 函数返回表示特定日期的连续序列号.例如,公式 =DATE(2008,7,8) 返回 2008-7-8或39637,取决于单元格格式,但空单元格计算和默认为日期格式. DATE也 ...
- bash 语法使用
1.定义函数时,不需要使用function作为函数的命名. 函数不需要形参. 函数名不能以数字作为开头 main() { in ) 1_start ;; ) 1_start 5_start ;; ) ...
- Android Activity的加载的模式
---恢复内容开始--- 本文来自http://www.cnblogs.com/lwbqqyumidi/p/3771542.html launchMode在多个Activity跳转的过程中扮演着重要的 ...
- Java NIO 系列教程
http://www.iteye.com/magazines/132-Java-NIO