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 ...
随机推荐
- [LUOGU] P2634 [国家集训队]聪聪可可
点分治裸题,甚至不需要栈回撤. 尝试用容斥写了一波,就是把所有子树混一块计算,最后减去子树内路径条数. #include<iostream> #include<cstring> ...
- pandas学习series和dataframe基础
PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...
- mysql EOF
mysql shell 执行脚本 #!/bin/bash export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql-5.6/bin:/usr ...
- html5音频audio对象封装成vue组件的方式调用以及setTimeout如何在vue2生效 (vue2正在熟悉中,ajax还是用jQuery来写舒服些,里面含有一些php写法可略过) 此网页应用在PC不考虑手机端
// vue2 组件封装如下: <template> <div> <div><!--vue element 组件的引用 Switch 开关 不懂请自行百度(重 ...
- GoF23种设计模式之行为型模式之解释器模式
一.概述 给定一种语言和其文法的一种表示,再定义一个解释器,该解释器使用表示来解释语言中的句子. 二.适用性 当需要解释一种语言,并且可以将该语言中的句子表示 ...
- F查询与Q查询
F查询 如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F() 的实例可以在查询中引用字段,来比较同一个 model 实例中两个不同字段的值. # 查询评论 ...
- for_each_node(node)
遍历各个pg_data_t节点. 1.定义在include/linux/nodemask.h中 /* * Bitmasks that are kept for all the nodes. */ en ...
- rs485多主
因复位时I/O口都输出高电平.如果把I/O口直接与RS-485接口芯片的驱动器使能端DE端相连,会在CPU复位其间DE为高,从而使本节点处于发送状态.如果此时总线上其它节点在发送数据,则此次数据传输将 ...
- 【ZABBIX】Linux下安装ZABBIX
说明:搭建ZABBIX所需的软件列表为:RHEL6.5+Nginx+MySQL+PHP+ZABBIX. 一.软件包 软件名称 版本 下载地址 nginx 1.10.3 http://nginx.org ...
- 【Shell】使用shell打印菜单,一键安装Web应用
问题描述: [解答] [root@A04-Test- scripts]# more menu.sh #!/bin/bash echo "1.[install lamp]" echo ...