leetcode 131. Palindrome Partitioning
生活随笔
收集整理的這篇文章主要介紹了
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--;}elsereturn 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)-1m=0while(m<n):if(s[m]!=s[n]):return Falseelse:m+=1n-=1return Truedef 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 中傳參數?
- foo=1 #?指向int數據類型的foo(foo 沒有類型)
- lfoo=[1]# 指向list類型的lfoo。
- 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, 所指向的數組對象并沒有變化, 只是數組內部發生了變化。
- python 中的參數傳遞可以理解為變量傳值操作
- def changeI(c):c=10if __name__=="__main__":# nums=[100, 4, 200,1,3,2]# s=Solution()# c=s.longestConsecutive(nums)a=2changeI(a)print(a)
a 輸出依然為2,對于一個int 對象2, 和指向它的變量a, 值傳遞,復制了變量a的值。a,c 指向同一個int 對象,c=10,int 不能改變,生成一個新的int 對象,c指向這個新對象,a指向的對象沒有變化。
- def changeI(c):c[0]=10if __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類型?
- 可取slide var2[1:5] ? 截取[:]
- +, in, not in, *2(重復輸出)
- string.capitalize()
- .center(width): 使居中
- .count(str, beg=0, end=len(string)) , str 出現的次數
- string.decode(encoding='UTF-8', errors='strict')
- string.endswith(obj, beg=0, end=len(string))
- string.find(str, beg=0, end=len(string))
- string.index(str, beg=0, end=len(string))跟find()方法一樣,只不過如果str不在 string中會報一個異常.
- string.isalnum()如果 string 至少有一個字符并且所有字符都是字母或數字則返
回 True,否則返回 False
- string.isdigit()
- string.islower()
- string.join(seq)
- string.replace(str1, str2,? num=string.count(str1))把 string 中的 str1 替換成 str2,如果 num 指定,則替換不超過 num 次.
?
python 中的list 類型?
python 中的set 類型?
- issubset()
- in , not in
- union, intersection, difference
- .discard(a)
- .remove(a) ?, 不存在時, 回報錯。
python 中的in? range 的范圍?
python 中的self?
?
轉載于:https://www.cnblogs.com/fanhaha/p/7406011.html
總結
以上是生活随笔為你收集整理的leetcode 131. Palindrome Partitioning的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU6156 Palindrome F
- 下一篇: okHttp通信