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 ...
随机推荐
- oracle 禁用索引
同步数据的时候 有索引会比较慢 可以暂时禁用索引 --禁用索引 ALTER INDEX PK_T_AUTH_USERROLE_ID UNUSABLE; --恢复索引ALTER INDEX UK_T_A ...
- 003-主流区块链技术特点及Hyperledger Fabric V1.0版本特点
一.Hyperledger fabric V1.0 架构 1.逻辑架构: 2.区块链网络 3.运行时架构 二.架构总结 1.架构要点 分拆Peer的功能,将Blockchain的数据维护和共识服务进行 ...
- Maven学习笔记—私服(包含maven的setting.xml配置)
为什么要用远程仓库(私服) 如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件,这样就加大了中央仓库 ...
- ThinkPHP框架基础知识三
一.JS文件与Css文件存放位置 其实JS与Css文件放在任意位置都可以找到,只要路径正确就行. 在TP框架中我们访问的所有文件都要走入口文件index.php,相当于访问的是index.php页面. ...
- web标准的理解
首先,什么是web标准?web标准是w3c组织为解决跨浏览器兼容问题而推出的关于网页开发时应遵守的规范.在网页的四个部分中网页的内容是由网页开发者自己定义的,因此这一部分无法标准化,而网页的结构(HT ...
- C# 字符串中正则表达式的应用
1.截取字符串中指定内容 {"weatherinfo":{"city":"北京","cityid":"1010 ...
- const修饰的常量 不能被直接修改 但是可以通过指针进行间接修改
大家都知道如下代码中,被const限定的a是不可以被直接修改的 void main() { const int a = 3; a=1; } 在C++中const修饰的常量,不能被直接修改,但是可以通过 ...
- JavaWeb HTTP
1. Http协议 1.1. 什么是Http协议 Http的全称为HyperText Transfer Protocol,译为超文本传输协议,是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通 ...
- Spark 实现自定义对象sequenceFile方式存储,读写示例(scala编写)
package com.fuge.bigdata.datahub.analysis import java.io.{DataInput, DataOutput} import com.fuge.big ...
- Spring_泛型依赖注入