LeetCode OJ-- Simplify Path **
https://oj.leetcode.com/problems/simplify-path/
对linux路径的规范化,属于字符串处理的题目。细节很多。
#include <iostream>
#include <string>
using namespace std; class Solution {
public:
string simplifyPath(string path) {
if(path == "/../")
return "/"; int len = path.length();
// if last was ..,then regard it as ../
if(len>= && path[len-]=='.'&& path[len-] == '.')
path.append("/");
len = path.length(); string ans = "/";
int pos = ;
int pos2;
while()
{
//unfind is -1
pos2 = path.find_first_of('/',pos); //two '//', ignore
if(pos2!= - && path[pos2-]=='/')
{
pos = pos2+;
continue;
}
//exit test,means not find
//handle the left eg:/a/b/cc
if(pos2 == - )
{
string subStr = path.substr(pos,pos2-pos+);
if(subStr == "." || subStr == "..")
break; ans.append(subStr);
break;
}
//if ../
if(pos2>= && path[pos2-] == '.' && path[pos2-] == '.' && pos2- == pos)
{
int pos3 = ans.rfind('/',ans.size()-);
if(pos3>=)
ans = ans.substr(,pos3+);
pos = pos2 +;
continue;
}
//if ./
if(pos2>=&&path[pos2-] == '.'&& pos == pos2-)
{
pos = pos2+;
continue;
} string subStr = path.substr(pos,pos2 - pos +);
ans.append(subStr);
pos = pos2 + ; }
//remove the last /
int t = ans.length()-;
while(t>)
{
if(ans[t]=='/')
t--;
else
break;
} ans = ans.substr(,t+); return ans;
}
}; int main()
{
class Solution myS;
cout<<myS.simplifyPath("/b/DfZ/AT/ya///./../.././..")<<endl;
return ;
}
LeetCode OJ-- Simplify Path **的更多相关文章
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- [LeetCode] 71. 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. For example, path = "/home/& ...
- Java for LeetCode 071 Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- Leetcode 之Simplify Path @ python
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- LeetCode OJ 112. 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#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
- leetcode[70] Simplify Path
题目的意思是简化一个unix系统的路径.例如: path = "/home/", => "/home"path = "/a/./b/../../ ...
- LeetCode OJ 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- redis学习笔记(3)
redis学习笔记第三部分 --redis持久化介绍,事务,主从复制 三,redis的持久化 RDB(Redis DataBase)AOF(Append Only File) RDB:在指定的时间间隔 ...
- destoon模块自定义字段的添加并让其支持搜索的方法
今天看了看模块设置里的自定义字段功能的用法,试着加了个新字段glry,设置了值,然后去数据库moduleid的article表看,字段成功加上了. 于是去template下article文件夹的lis ...
- 【mysql】【转发】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,I
[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for o ...
- 通过源码编译安装VIM
开发中使用的是Ubuntu 12.04 LTS,通过sudo apt-get install vim安装的版本较低,不支持YCM,所以,用源码编译并安装最新的Vim. 卸载旧版本的Vim: sudo ...
- django知识分支_1
django知识分支 1.Cookie工作流程: 浏览器向服务器发出请求,服务器接收到浏览器的请求进行处理,服务器设置一个cookie发送给浏览器,浏览器将cookie保存,当需要再次登录的时候,浏览 ...
- manjaro linux没有ll等命令的解决办法
编辑~/.bashrc, 添加alias 如下 vim ~/.bashrc设置别名. 添加如下行 alias ll='ls -alF' alias la='ls -A' alias vi='vim' ...
- HUD:2853-Assignment(KM算法+hash)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Assignment Time Limit: 2000/1000 MS (Java/Others) ...
- dubbo基础文档
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个应用, ...
- linux随笔二
1.查看整个文件 cat mongo.sh 查看脚本文件的内容:mongo 172.60.0.203:27017/che001 -uplatform -pplatform cat -n **,查 ...
- 第4章--变量,作用域和内存问题 jquery
4.1基本类型和引用类型的值 解析器要分析赋给变量的值是基本类型值还是引用类型的值 基本类型:undefined null boolean number string 引用类型的值: ...