LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
简化路径,linux下面简单的路径操作而已,代码如下:
class Solution {
public:
string simplifyPath(string path) {
stack<string> tokenStk;
int sz = path.size();
for (int i = ; i < sz; ++i){
if (path[i] == '/') continue;
else{
string tmp = "";
for (int j = i; j < sz && path[j] != '/'; ++j, ++i){
tmp.append(, path[i]);
}
if (tmp == ".."){
if (!tokenStk.empty())tokenStk.pop();
}
else if (tmp == ".")
continue;
else
tokenStk.push(tmp);
}
}
vector<string> tokenVec;
while (!tokenStk.empty()){//存储的是反向的目录,将其输出打vector中,这样比较好
tokenVec.push_back(tokenStk.top());
tokenStk.pop();
}
string ret;
if (tokenVec.empty()) ret.append(, '/');
for (int i = tokenVec.size() - ; i >= ; --i){
ret.append(, '/');
ret.append(tokenVec[i]);
}
return ret;
}
};
PS:应为短的if不喜欢加上{}的原因,找bug找了好久,下次不能继续这个坏习惯,mark一下。
下面是java版本,不得不说java的String的api比c++的要好用太多了,可以用正则表达式分割单词真是爽啊,不用向c++那样做很多次判断了,代码如所示:
public class Solution {
public String simplifyPath(String path) {
String[] pathArr = path.split("[//]+");//首先用正则表达式将整个式子按照//分开
Stack<String> s = new Stack<String>();
for(int i = 0; i < pathArr.length; ++i){
if(pathArr[i].equals("..")){
if(!s.isEmpty())
s.pop();
}else if(pathArr[i].equals(".")){
continue;
}else{
if(!pathArr[i].equals(""))//split有可能会分割出来空白的字符,这里应该注意并且剔除
s.push(pathArr[i]);
}
}
String ret = new String("");
while(!s.isEmpty()){
ret = "/" + s.pop() + ret;
}
if(ret.length() == 0)
ret += "/";
return ret;
}
}
LeetCode OJ:Simplify Path(简化路径)的更多相关文章
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- lintcode 中等题:Simplify Path 简化路径
题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【LeetCode每天一题】Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...
- 071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...
- Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- LeetCode OJ:Path Sum II(路径和II)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode OJ:Path Sum(路径之和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 008-CentOS添加环境变量
在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时, ...
- MariaDB复制架构中应该注意的问题
一.复制架构中应该注意的问题: 1.限制从服务器只读 在从服务器上设置read_only=ON,此限制对拥有SUPPER权限的用户均无效: 阻止所有用户(在从服务器执行一下命令并保持此线程,也就是执行 ...
- appium实现adb命令 截图和清空EditText
原文地址http://www.cnblogs.com/tobecrazy/p/4592405.html 原文地址http://www.cnblogs.com/tobecrazy/ 该博主有很多干货,可 ...
- 【转】XML的几种读写
XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...
- django【orm操作】
一.ORM表结构 class Publisher(models.Model): name = models.CharField(max_length=30, verbose_name="名称 ...
- vue之 node.js 的简单介绍
一.什么是 node.js? 它是可以运行在JavaScript的服务平台 二.安装 1.node.js的特性 - 非阻塞IO模型 - 时间驱动 2.运用场景 - 高并发低业务 - 实时场景 - 聊天 ...
- beego——事务处理和命令模式
1.事务处理 ORM 可以简单的进行事务操作. o := NewOrm() err := o.Begin() // 事务处理过程 ... ... // 此过程中的所有使用 o Ormer 对象的查询都 ...
- LightOJ - 1138 (二分+阶乘分解)
题意:求阶乘尾部有Q(1 ≤ Q ≤ 108)个0的最小N 分析:如果给出N,然后求N!尾部0的个数的话,直接对N除5分解即可(因为尾部0肯定是由5*2构成,那么而在阶乘种,2的因子个数要比5少,所以 ...
- 【Head First Servlets and JSP】笔记20:EL以及<jsp:useBean ....>的补充
1.EL的英文是Expression Language,译成中文就是“表达式语言”.这是一种给前端程序员使用的脚本语言,EL与Java表达式相比并没有什么“天壤之别”,在后端程序员看来多少有点“多此一 ...
- SVN使用—工作模式及运行原理以及优缺点对比
一.SVN原理 (1)运行方式 svn服务器有2种运行方式:独立服务器和借助apache运行. 1.独立服务器访问 访问地址如:svn://svn.test.com/test 2.借助Apache等h ...