leetcode 131. Palindrome Partitioning

简单的思路:

深度优先搜索:

 

class Solution {
public:
    
    bool isPalin(string str)
    {
        int n=str.length();
        int i=0;
        int j=n-1;
        while(i<j)
        {
            if(str[i]==str[j])
            {
                i++;
                j--;
            }
            else
                return false;
        }
        return true;
    }
    void getPart(string s, vector<vector<string>>& result, vector<string> temp)
    {
        if(s.length()==0)
        {
            result.push_back(temp);
            return;
        }
        string str1;
        string str2;
        for(int i=1;i<=s.length();i++)
        {
            str1=s.substr(0,i);
          // cout<<str1<<endl;
            if(isPalin(str1))
            {
                str2=s.substr(i);
             //   cout<<str2<<endl;
                temp.push_back(str1);
                getPart(str2, result, temp);
                temp.pop_back();
            }
        }
    }
    
    vector<vector<string>> partition(string s) {
        vector<vector<string>> result;
        vector<string> temp;
        getPart(s, result, temp);
        return result;
        
    }
};

  

python

class Solution(object):
    
    def isPalin(self, s):
        n=len(s)-1
        m=0
        while(m<n):
            if(s[m]!=s[n]):
                return False
            else:
                m+=1
                n-=1
        return True
    def dfs(self,s, stringlist):
        if len(s)==0: Solution.result.append(stringlist)
        for i in range(1, len(s)+1):
            if(self.isPalin(s[:i])):
                self.dfs(s[i:],stringlist+[s[:i]])
    
    
    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        
        """
        Solution.result=[]
        self.dfs(s,[])
        return Solution.result

  

python 中传参数?

  1. python 中对象有类型,变量没有类型。所有的变量都可以理解为一个对象的“引用”。类似c中的void *类型。
  • foo=1 # 指向int数据类型的foo(foo 没有类型)
  • lfoo=[1]# 指向list类型的lfoo。
  1. 可更改对象与不可更改对象
  • python中 strings, tuples, numbers 不可更改,
  • list, dict 可更改
  • foo=1
  • foo=2   内存中原始1 对象不可变,foo指向一个新的int 对象,
  • lfoo=[1]
  • lfoo[0]=2 更改list 中的第一个元素,list 可变,所有第一个元素变为2。 lfoo 指向一个包含一个对象的数组。 赋值时,是一个新int 对象被指定对lfoo 所指向的数组对象中的第一个元素。但对于lfoo, 所指向的数组对象并没有变化, 只是数组内部发生了变化。

  1. 参数传递
  • python 中的参数传递可以理解为变量传值操作
  • def changeI(c):
        c=10
        
    
        
    if __name__=="__main__":
        
      #  nums=[100, 4, 200,1,3,2]
     #   s=Solution()
      #  c=s.longestConsecutive(nums)
        a=2
        changeI(a)
        print(a)
        
    

      a 输出依然为2,对于一个int 对象2, 和指向它的变量a, 值传递,复制了变量a的值。a,c 指向同一个int 对象,c=10,int 不能改变,生成一个新的int 对象,c指向这个新对象,a指向的对象没有变化。

  • def changeI(c):
        c[0]=10
        
    
        
    if __name__=="__main__":
        
      #  nums=[100, 4, 200,1,3,2]
     #   s=Solution()
      #  c=s.longestConsecutive(nums)
        a=[2]
        changeI(a)
        print(a[0])
    

      输出10,变量依旧是传值,复制a的值,a和c 指向同一个对象,但是list 可改变对象,对 c[0]的操作,就是对a的操作。

python 中的string类型?

 

python 中的list 类型?

python 中的set 类型?

  • issubset()
  • in , not in
  • union, intersection, difference
  • .discard(a)
  • .remove(a)  , 不存在时, 回报错。

python 中的in? range 的范围?

python 中的self?

 

posted @ 2017-08-21 18:24  hahahaf  阅读(383)  评论(0编辑  收藏  举报