【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 ...
随机推荐
- ListView知识点汇总(9.2)
1 最为基础的listview: http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html http://blog.csdn.net/h ...
- centos源码安装lnmp
参考博客:http://blog.csdn.net/yanzi1225627/article/details/49123659 服务器环境为:CentOS6.6 64位(虚拟机) 一.安装前准备 创建 ...
- 正则表达式 判断IP 数字
1.正则表达式 public static bool checkIP(string strIP) { //string regex = @"^(2[0-4]\d | 25[0-5] | [0 ...
- .net 哈希表和字典的基本用法
哈希表 传送门:https://www.cnblogs.com/xpvincent/archive/2013/01/15/2860841.html using System; using System ...
- Warning: (3719, “‘utf8’ is currently an alias for the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.”)
[1]本地版本 Mysql 8.0.12 创建表SQL: DROP TABLE IF EXISTS students; CREATE TABLE `students` ( `sId` ) UNSIGN ...
- mysql 考勤表异常 【待修改】
有考勤刷卡记录表,表名为attendance ,有如下字段: 姓名 卡号 刷卡时间 刷卡类型 name id time type 张三 59775623 2010-04-01 07:23:37 ...
- [转]mysqlx 同时使用 AND OR
- 线段树专题—HDU1698 Just a Hook
题意:t组数据,给一个n.m表示n长度的钩和m次操作.初始钩子的每单位长度的价值为1,接下来输入 x,y,k 的操作把钩子[x,y]区间的价值替换为k,求m次操作后钩子的价值为多少 分析:成段替换.最 ...
- 向MapReduce转换:通过部分成绩计算矩阵乘法
代码共分为四部分: <strong><span style="font-size:18px;">/*** * @author YangXin * @info ...
- billboard因为合批导致出问题的一个想法
由于unity中距离较近的2个billboard物体会动态合批,如果缩放不同,显示就有问题.还得在shader中"DisableBatching"="true" ...