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 ...
随机推荐
- Spring AOP注解形式简单实现
实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...
- destoon手机端mobileurl函数增加城市分类参数
mobileurl函数在include/global.func.php 858行 共四个参数,moduleid-模型id,catid-分类id,itemid -文章id,page-页码 functio ...
- selenium2-框架思想介绍
为什么要有框架? 可维护性 提高编写脚本效率 提高脚本的可读性 框架的几大要素: 1. driver管理 2. 脚本 3. 数据 4. 元素对象 5. Log 6. 报告 7. 运行机制 8. 失败用 ...
- ModelViewSet的继承关系
- dedecms 建站相关问题
1.栏目新建文章提示:模板文件不存在,无法解析文档! 解决方法:把模板文件使用".html"的格式 /include/arc.archives.class.php 556行 if ...
- js实现获取当前时间是本月第几周的方法
这篇文章主要介绍了js实现获取当前时间是本月第几周的方法,涉及javascript针对日期及时间的相关操作技巧,非常简单实用,需要的朋友可以参考下. 本文实例讲述了js实现获取当前时间是本月第几周的方 ...
- git仓库删除所有提交历史记录
stackoverflow原问题地址:http://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-g ...
- luogu2153 [SDOI2009]晨跑
要想限制流量,总要想着拆点. #include <iostream> #include <cstring> #include <cstdio> #include & ...
- Vmware复制完好的linux目录后网卡操作
目录 Vmware复制完好的linux目录后网卡操作 修改/etc/udev/rules.d/70-persistent-net.rules 修改网卡配置文件 重启查看 Vmware复制完好的linu ...
- getattr、setattr、hasattr
写一个演示类 class test(): title="验证getattr.setattr.hasattr方法" def run(self): return "run方法 ...