Simplify Path

本文介绍了一种用于简化Unix风格文件路径的算法。该算法能够处理包括连续斜杠、上级目录引用在内的多种特殊情况,并通过使用字符串分割和vector容器来实现简洁高效的路径简化。

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

click to show corner cases.

Corner Cases:

 

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".
参考:http://blog.youkuaiyun.com/maverick1990/article/details/23275051
题意:简化一个Unix文件路径,注意:
(1)"/." 表示本级目录,可直接忽略
(2)"/.." 表示返回上一级目录,即若上一级目录存在,连同"/.."一并删除,否则只删除/..
(3)若去除冗余后路径为空,返回"/"
(4)若包含连续"/",删除多余的
(5)若路径不是单个“/”,删除路径最后一个“/”
分析:一开始用逐个字符判断的方法,考虑很多边界条件。后来用字符串分割的思想,比较简明。思路如下:
(1)用“/”分割字符串,遍历每个分割部分,存入一个vector<string>中
(2)若当前分割部分为空,证明有连续的"/"或是最后一个“/”,忽略
(3)若当前部分为“.”,忽略
(4)若当前部分为“..”,若vector不为空,去除vector最后一个元素
(5)再将vector中的string用“/”连起来,得到结果
 
C++实现代码:
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;

class Solution
{
public:
    string simplifyPath(string path)
    {
        if(path.empty())
            return "";
        vector<string> ret;
        string tmp;
        stringstream ss(path);
        while(getline(ss,tmp,'/'))
        {
            if(tmp.empty()||tmp==".")
                continue;
            if(tmp=="..")
            {
                if(!ret.empty())
                    ret.pop_back();
            }
            else
                ret.push_back(tmp);
        }
        tmp.clear();
        for(int i=0; i<(int)ret.size(); i++)
        {
            tmp+="/";
            tmp+=ret[i];
        }
        if(ret.empty())
            return "/";
        return tmp;
    }
};

int main()
{
    Solution s;
    string ss="";
    cout<<s.simplifyPath(ss)<<endl;
}

 第二遍

class Solution {
public:
    string simplifyPath(string path)
    {
        if(path.empty())
            return "";
        string res="/";
        vector<string> vec;
        istringstream ss(path);
        string tmp;
        while(getline(ss,tmp,'/'))
        {
            if(tmp.empty()||tmp==".")
                continue;
            if(tmp==".."&&!vec.empty())
            {
                vec.pop_back();
                continue;
            }
            else if(tmp!="..")
                vec.push_back(tmp);
        }
        int i=0;
        while(i<vec.size())
        {
            res+=vec[i];
            ++i;
            if(i<vec.size())
                res+='/';
        }
        return res;
    }
};

 

### Simplify3D 使用教程 #### 一、界面操作基础 对于初次接触Simplify3D的用户来说,了解其基本的操作方式至关重要。通过鼠标的不同按键可以实现多种视角调整功能: - 右键用于移动视野[^4]。 - 左键负责旋转视野[^4]。 - 中键以及滚轮均可用来恢复默认视图[^3]。 这些简单的交互手段能够帮助使用者快速上手并熟悉软件环境。 #### 二、打印机配置初始化 首次启动该程序时需完成一个重要步骤——定义所使用的3D打印设备规格。具体做法如下:点击菜单栏中的“工具”,随后选择“选项”。在此处输入或修改当前连接至计算机上的实际硬件参数,比如尺寸等信息。这一步骤确保了后续所有的切片处理都能精准匹配特定型号的需求。 #### 三、单位设置建议 为了使各项参数更加直观易懂,在速度方面推荐采用毫米每秒作为计量标准。这种标准化的做法不仅有助于提高工作效率,同时也减少了因误解而产生的错误风险。 #### 四、模型加载流程 当一切准备就绪之后就可以着手导入待加工的对象文件了。通常情况下只需按照提示依次执行相应命令即可顺利完成这一过程。 ```python # 假设有一个Python脚本辅助自动化某些重复性的任务 import os def load_model(file_path): """模拟加载STL或其他支持格式的3D模型""" if not os.path.exists(file_path): raise FileNotFoundError(f"The specified file does not exist: {file_path}") print(f"Loading model from '{file_path}'...") # 这里省略具体的加载逻辑... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值