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. pandas关联mysql并读写数据库

    1.代码读写mysql,必须安装关联mysql的工具 操作如下命令: sudo apt-get install mysql-server mysql-clientsudo apt-get instal ...

  2. ubuntu16.04安装 java JDK8

    安装openjdk1.更新软件包列表: sudo apt-get update 2.安装openjdk-8-jdk: sudo apt-get install openjdk-8-jdk 3.查看ja ...

  3. html5音频audio对象处理以及ios微信端自动播放和息屏后唤醒的判断---可供参考(功能都完整实现了,只是细节还没处理的很好)

    // html模版中的 此处结合了weui样式整合的微信手机端片段代码(不可直接粘贴复制进行使用)里面含有一些php的写法,可直接略过..###重点参考js代码### <div> < ...

  4. LeetCode(225) Implement Stack using Queues

    题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...

  5. LeetCode(283)Move Zeroes

    题目 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...

  6. 2015四川省赛 D Vertex Cover 搜索

    题意: 给出一个\(n\)个点\(m\)条边的无向图,现在要给若干个点染色,使得每条边都至少邻接一个被染色的顶点.问至少要给多少各点染色才能满足条件. 分析: 注意到题目中有一个很特殊的条件: 对于图 ...

  7. linux 复制部分文件到另外的文件夹

    show the command: |xargs -i cp {} ../ 或者指定目录 |xargs -i cp {} /home/peter

  8. [git 学习篇]远程创库

    实际情况往往是这样,找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交. 完全 ...

  9. TOJ1698: Balanced Lineup

    Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...

  10. javascript学习笔记 - 执行环境及作用域

    一 执行环境(环境) 1.每个执行环境都有一个关联的全局变量对象.例如:web浏览器中,window对象为全局变量对象.环境中定义的所有变量和函数都保存在该对象中.全局执行环境是最外围的环境. 2.执 ...