zoukankan      html  css  js  c++  java
  • [leetcode]Palindrome Partitioning

    暴力= =

    枚举断开位置...

    据说可以dp,等下试试

    class Solution {
    public:
        bool isPal(const string& s , int start , int end){
            end --;
            while(start < end){
                if(s[start] != s[end]) return false;
                start ++ ; end --;
            }
            return true;
        }
        void search(const string& s , vector<vector<string> > &ans , vector<string>& path , int prev , int end){
            if(end >= s.size()){
                if(isPal(s , prev , end)){
                    path.push_back(s.substr(prev , end-prev));
                    ans.push_back(path);
                    path.pop_back();
                }
                return;
            }
            
            //not break
            search(s , ans , path , prev , end + 1);
            //break
            if(isPal(s , prev , end)){
                path.push_back(s.substr(prev,end-prev));
                search(s , ans , path , end  , end + 1);
                path.pop_back();
            }
        }
        
        vector<vector<string>> partition(string s) {
            
            vector<vector<string> >ans;
            if(s.size() == 0) return ans;
            vector<string> path;
            search(s , ans , path , 0 , 1);
            return ans;
        }
    };
  • 相关阅读:
    AJAX 验证用户名是否存在
    字符串变量中保存路径
    hdu Kaitou Kid The Phantom Thief (2)
    hdu 连连看
    hdu Sequence one
    hdu Sticks
    hdu Nightmare
    hdu Sudoku Killer
    hdu Dota all stars
    hdu Remainder
  • 原文地址:https://www.cnblogs.com/x1957/p/3498403.html
Copyright © 2011-2022 走看看