LeetCode--字符串

Posted zhuifeng-mayi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode--字符串相关的知识,希望对你有一定的参考价值。

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;
    
;

 

以上是关于LeetCode--字符串的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-字符串转整数

[Leetcode Weekly Contest]272

leetcode: 字符串

LeetCode Word Ladder II

LeetCode 680. 验证回文字符串 Ⅱ

leetcode-791. 自定义字符串排序