从热电偶到加速度计:搞懂传感器信号类型,是选单端还是差分接线的第一步
2026/5/1 17:53:31
给定一个 Unix 风格的绝对路径path,请将其化简为规范路径。规则如下:
/视为一个/.表示当前目录,忽略..表示返回上一级目录(若已在根目录/,则保持/)输出要求:
/开头/分隔/(除非输出就是根目录/)把路径按/切成一个个目录片段(token),用vector<string>充当“栈”:
/或出现//→ 跳过.:当前目录 → 跳过..:返回上级 → 栈非空则pop_back()push_back()最后把栈中的目录按顺序拼接回去:
//" + dir1 + "/" + dir2 + ...输入:/home/
输出:/home
输入:/a/./b/../../c/
输出:/c
输入:/../
输出:/
输入:/home//foo/
输出:/home/foo
#include<string>#include<vector>usingnamespacestd;classSolution{public:stringsimplifyPath(string path){vector<string>st;string name;intlen=static_cast<int>(path.size());for(inti=0;i<=len;++i){charc=(i==len)?'/':path[i];if(c=='/'){//name为空有2种情况,一种就是开始就是 '/'//另一种就是连续的 '/'(因为上次name被clear了)if(name.empty())continue;if(name=="."){}elseif(name==".."){if(!st.empty())st.pop_back();}else{st.emplace_back(name);}name.clear();}else{name.push_back(c);}}if(st.empty())return"/";string res;for(constauto&s:st){res.push_back('/');res+=s;}returnres;}};