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 **的更多相关文章

  1. 【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 ...

  2. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...

  3. [LeetCode] 71. Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  4. 【leetcode】Simplify Path

    题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...

  5. Java for LeetCode 071 Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...

  6. Leetcode 之Simplify Path @ python

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  7. 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 ...

  8. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

  9. leetcode[70] Simplify Path

    题目的意思是简化一个unix系统的路径.例如: path = "/home/", => "/home"path = "/a/./b/../../ ...

  10. 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 ...

随机推荐

  1. Python爬虫系列-PyQuery详解

    强大又灵活的网页解析库.如果你觉得正则写起来太麻烦,如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的最佳选择. 安装 pip3 install ...

  2. 【android】安卓开发apk列表

    - 谷歌的Zxing框架的扫码软件 (目前国内的应用商店很少此种类型的扫码app) - 解析IP地址功能,从IP地址(子网掩码)自动解析出网段,广播地址

  3. 谈谈你对Hibernate的理解

    答: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管程序运行的过程和操作怎么样,本质上都是要得到一 ...

  4. leetcode-25-exercise_string&array

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  5. centos7 bond 和 网桥配置

    rhel7系统bond配置(更新版本):https://www.cnblogs.com/zhangjianghua/p/9119808.html Bonding的模式一共有7种: 1.mode=0(b ...

  6. Java中的数据类型和引用

    JAVA数据类型分primitive数据类型和引用数据类型. Java中的primitive数据类型分为四类八种.primitive也不知道怎么翻译比较贴切, 暂且叫他基本数据类型吧, 其实直接从英文 ...

  7. Activity树图

  8. Linux的档案权限与目录配置

    重点回顾:1.Linux的每个档案中,依据权限分为使用者.群组与其他人三种身份 2.群组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个群组的支持 3.利用"ls -l ...

  9. JavaScript: 2015 年回顾与展望

    链接:http://www.sitepoint.com/javascript-2015-review/ JavaScript经历了一个不平凡的一年.尽管到5月份已经20年了,关于JS的新闻.项目和兴趣 ...

  10. 解决MySQL版本之间造成的乱码、数据查询不出的问题

    在数据库连接字符串上加  charset=utf8 <connectionStrings> <add name="XJRDSModels" connectionS ...