【leetcode刷题笔记】Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
- 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"
.
题解:
- 用split函数将path按照一个或者多个(正则表达式实现)'/'分成不同的字符串存放在数组positions中。
- 遍历positions,如果遇到非"..",".",""的字符串,就放入answers列表中;如果遇到".."且answers不为空,就把answers尾部的字符串扔掉。
- 最后把answers中的字符串用"/"连接起来,注意在连接后的字符串结尾会多一个"/',可以substring一下result去掉。
代码如下:
public class Solution {
public String simplifyPath(String path) {
ArrayList<String> answers = new ArrayList<String>();
String[] positions = path.split("/+");
for(int i = 0;i < positions.length;i++){
if(positions[i].equals("..")){
if(answers.size() > 0)
answers.remove(answers.size()-1);
}
else if(!positions[i].equals(".") && !positions[i].equals(""))
{
answers.add(positions[i]);
}
} String result = new String("/");
for(String s:answers)
result += s + "/"; if(result.length() > 1)
result = result.substring(0,result.length()-1); return result;
}
}
【leetcode刷题笔记】Simplify Path的更多相关文章
- 【leetcode刷题笔记】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- LeetCode 刷题笔记 (树)
1. minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...
随机推荐
- Android下Sqlite的使用(9.7)
1 http://blog.csdn.net/liuhe688/article/details/6715983 2 http://www.eoeandroid.com/thread-170715-1- ...
- Delphi中array of const应用
Delphi的Format函数大家都用得很多,第二个参数用着确实很方便.最近在数据库开发应用中需要自己创建一个带array of const参数的函数,对于常用的类型String,Integer,Po ...
- Memcache针对不同场景数据应用缓存策略
Memcache主要的作用是为减轻大访问量对数据库的冲击,所以一般的逻辑是首先从memcache中读取数据,如果没有就从数据库中读取数据写入到memcache中,等下一次读取的时候就可以从memcac ...
- shell 判断问题总结
#!/bin/bash #比如需要判断一个变量是否含有值: if [[ -z $1 ]] ; thenecho "Something like empty!"exit 0;fi # ...
- vim 基本常用设置
1. :set number 设置行号 2. :set relativenumber 设置相对行号 3. :set hlsearch 设置搜索高亮 4. :set noh 关闭搜索高亮 ...
- notepad 替换行收尾字符串或在行首尾新增字符
用 Notepad++ 打开,把每一个将要放在表中单元格的内容放一行(注: ^ 代表行首 $ 代表行尾) 去除行尾空格和空白行:按CTRL+H 选择正则表达式-- 查找目标:\s+$ 替换为空 去除行 ...
- 第1章列表处理——1.1 Lisp列表
Lisp是啥? Lots of Isolated Silly Parentheses (大量分离的愚蠢的括号) Lisp指的是"LISt Processing"(列表处理),通过把 ...
- 题目3 : Fibonacci
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given a sequence {an}, how many non-empty sub-sequence of it ...
- Google Code Jam 2014 资格赛:Problem B. Cookie Clicker Alpha
Introduction Cookie Clicker is a Javascript game by Orteil, where players click on a picture of a gi ...
- maven 依赖文件 pom.xml 编译 mvn compile 运行 不用mvn exec:java -Dexec.mainClass="hello.HelloWorld" 打成jar包 mvn package mvn install http://blog.csdn.net/yaya1943/article/details/48464371
使用maven编译Java项目 http://blog.csdn.net/yaya1943/article/details/48464371 使用"mvn clean"命令清除编 ...