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 中传参数?
- python 中对象有类型,变量没有类型。所有的变量都可以理解为一个对象的“引用”。类似c中的void *类型。
- 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相关推荐
- 【回文串4 DFS】LeetCode 131. Palindrome Partitioning
LeetCode 131. Palindrome Partitioning DFS的经典套路题目!!! 八皇后问题写法类似!!! Solution1: 转载网址:http://www.cnblogs. ...
- [Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning
136. Palindrome Partitioning / 131. Palindrome Partitioning 本题难度: Medium Topic: Search DFS Descripti ...
- leetcode 131. Palindrome Partitioning | 131. 分割回文串(递归解法)
题目 https://leetcode.com/problems/palindrome-partitioning/ 题解 中规中矩的递归,因为每一层都需要在上一层保存好的 list 的基础上继续,而且 ...
- 【回文串5 重点+动态规划】LeetCode 132. Palindrome Partitioning II
LeetCode 132. Palindrome Partitioning II Solution1:我的答案1 直接模仿131那道题的DFS解法,找其中size最小的.果不其然,因为超时只能部分AC ...
- [Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 131. Palindrome Partitioning
文章目录 1 题目理解 2 回溯 3 动态规划 1 题目理解 输入:字符串s 规则:将字符串s分割,分割后每一个部分都是一个回文串. 输出:所有的分割方式 Example 1: Input: s = ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- LeetCode Palindrome Partitioning II
原题链接在这里:https://leetcode.com/problems/palindrome-partitioning-ii/ 题目: Given a string s, partition s ...
最新文章
- Laravel源码解析之从入口开始
- 【知识星球】softmax损失相关的小问题
- 你不是一个人在战斗!有人将吴恩达的视频教程做成了文字版
- 从零开始使用Skywalking分布式链路追踪系统
- 注入dll到explorer.exe中无反应_MBR膜生物反应器的安装及技术要求都有什么呢?
- 第4章 springboot热部署 4-1 SpringBoot 使用devtools进行热部署
- 风力涡轮机巨头维斯塔斯遭网络攻击
- python的skimage库 图像中值滤波;均值滤波;极大值滤波
- JPA EntityManager –HibernateEntityManager
- 如何下载Mysql安装包?
- Kepware与C#
- 近世代数 [计算机数学专题(3)]
- 搭建Linux环境学习C语言
- 随记:PNP和NPN三极管区别
- 孝当先健康管理品牌连锁项目说明会-南昌站圆满结束
- java 英语单词拼写训练_java 英文单词拼写纠正框架(Word Checker)
- CSS的动画特效(animation)
- 基于socket的联机五子棋
- 工作站压力测试软件,胜任多种工作负载 联想P500工作站评测
- SecureCRT连接服务器报错Failed to open the host key database file解决方法
热门文章
- 线性表的动态顺序存储和实现(C语言实现)【线性表】(4)
- 详解java方法与递归
- Oracle 段区块 分配 (2)
- linux如何找到桌面,我怎样才能找到我正在使用的桌面环境?
- slim android7 nexus7,【畅玩7.0】加一直升pure nexus 7.0系统简单教程(1106更新)
- bgb邻居关系建立模型_学习开发知识图谱中的长期关系依赖
- 【收藏】Linux系统常用命令速查手册(附赠PDF档)
- SSH运维总结-【liunx学习】
- php网站同时在线人数,也谈php网站在线人数统计
- php文章编辑页面,zblogphp在文章页面中增加直达后台编辑该文章内容功能