1、给定字符串s,分区s使得分区的每个子字符串都是回文。
返回s的所有可能的回文分区。
例如,给定s =“aab”,
返回
[
["aa","b"],
["a","a","b"]
]
class Solution {
public:
void dfs(string s,vector<string>&path,vector<vector<string>>&res){
if(s.size() < 1){
res.push_back(path);
return;
}
for(int i = 0;i<s.size();i++){
int begin = 0;
int end = i;
while(begin < end){
if(s[begin] == s[end]){
begin++;
end--;
}
else
break;
}
if(begin >= end){
path.push_back(s.substr(0,i+1));
dfs(s.substr(i+1), path,res);
path.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>>res;
vector<string>path;
dfs(s,path,res);
return res;
}
};
2、给定文件的绝对路径(Unix风格),简化它。
例如,
path =“/ home /”,=>“/ home”
path =“/ a /./ b /../../ c /”,=>“/ c”
单击以显示角落案例。
角落案例:
您是否考虑过path =“/../”的情况?
在这种情况下,您应该返回“/”。
另一个极端情况是路径可能包含多个斜杠'/',例如“/ home // foo /”。
在这种情况下,您应该忽略冗余斜杠并返回“/ home / foo”。
class Solution {
public:
string simplifyPath(string path) {
string res,tmp;
vector<string> stk;
stringstream ss(path);
while(getline(ss,tmp,'/'))
{
if (tmp == "" or tmp == ".")
continue;
if (tmp == ".." and !stk.empty())
stk.pop_back();
else if (tmp != "..")
stk.push_back(tmp);
}
for(auto str : stk)
res += "/"+str;
return res.empty() ? "/" : res;
}
};
来源:https://www.cnblogs.com/zhuifeng-mayi/p/11161973.html