Palindrome Subarrays
给定输入字符串,要求判断任意子字符串是否对称。
基本思路就是DP
写出DP表达式为 dp[i][j] = dp[i + 1][j - 1] && (s[i] == s[j]) dp[i][j]代表s(i,j)的子串是否对称
注意在for循环赋值时,这里增长的对象其实是子串的长度。
长度为奇数时:
(->代表决定)
dp[0,0]
dp[1,1] -> dp[0,2]
dp[2,2] -> dp[1,3] -> dp[0,4]
dp[3,3] -> dp[2,4] -> dp[1, 5]
...
长度为偶数时:
dp[0,1]
dp[1,2] -> dp[0,3]
dp[2,3] -> dp[1,4] -> dp[0,5]
dp[3,4] -> dp[2,5] -> dp[1,6]
...
因此外层循环变量是长度,内层循环变量是起点
def palindrom(s):
length = len(s)
# Create a 2D array in Python
dp = [[False for i in range(length)] for j in range(length)]
# length = 1 substring
for i in range(length):
dp[i][i] = True
# length = 2 substring
for i in range(length - 1):
if s[i] == s[i + 1]:
dp[i][i + 1] = True
# length = k substring
for len3 in range(3, length + 1):
for start in range(0, length - len3 + 1):
end = start + len3 - 1
if s[start] == s[end] and dp[start + 1][end - 1]:
dp[start][end] = True print dp
print dp[0][length - 1] palindrom('aaaaabbb')
Palindrome Subarrays的更多相关文章
- Palindrome Partitioning 解答
Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- 完美:adobe premiere cs6破解版下载[序列号+汉化包+破解补丁+破解教程]
原文地址:http://blog.sina.com.cn/s/blog_6306f2c60102f5ub.html 完美:adobe premiere cs6破解版下载,含序列号.汉化包.注册机.破解 ...
- JS控制菜单样式切换
$('#subtabs a').each(function (i, ele) { var href = $(ele).attr("href"); if (location.href ...
- JVM运行时内存结构
原文转载自:http://my.oschina.net/sunchp/blog/369707 1.JVM内存模型 JVM运行时内存=共享内存区+线程内存区 1).共享内存区 共享内存区=持久带+堆 持 ...
- python中os模块常用方法
#!/usr/bin/python## os module test import os print 'os.name: ', os.nameprint 'os.getcwd(): ', os.get ...
- OC教程10-NSNumber具体
NSNumber简单介绍 NSNumber是数字的对象形式,由于在OC的数组和字典中仅仅同意存放对象,所以我们有时候须要转化 我们普通的类型是 123 那么 NSNumber类型的是 @123, ...
- C#获取时间戳的方法
获取时间戳的方法 /// <summary> /// 获取时间戳 /// </summary> /// <param name= ...
- Sqlserver统计语句
--查看被缓存的查询计划 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED st.text AS [SQL] , cp.cacheobjtype , c ...
- 除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
在 SELECT 后加 TOP 100 PERCENT .
- MS-SQL数据库备份方法
一.手动备份 打开企业管理器 --> 右键点击需要备份的数据库 --> 所有任务 --> 备份数据库 或者: 查询分析器: use master backup database 数 ...
- Jquery:jquery中的DOM操作<一>
之前两天学习了Jquery强大的选择器,今天学习了一部分Jquery对DOM的操作,下面我将把自己今天的成果分享给大家,那些菜鸟们,你们是否需要巩固之前所学? 首先需要知道,DOM操作分为3个方面:D ...