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 ...
随机推荐
- poj 2718 Smallest Difference(穷竭搜索dfs)
Description Given a number of distinct , the integer may not start with the digit . For example, , , ...
- PHP设计模式笔记七:观察者模式 -- Rango韩老师 http://www.imooc.com/learn/236
观察者模式 概述: 1.观察者模式(Observer),当一个对象状态发生改变时,依赖他的对象全部会收到通知,并自动更新 2.场景:一个事件发生后,要执行一连串更新操作,传统的编程方式,就是在事件的代 ...
- Javascript:一款简易的图片切换插件
最近迷上javascript,每天不写点什么都不舒服哈~ 尽管自己能力有限,还是尽自己所能写点东西出来. 实现效果: 效果预览:http://codepen.io/anon/pen/BNjxXj 该插 ...
- 最全的js正则表达式用法大全
匹配中文字符的正则表达式: [u4e00-u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串的长度(一 ...
- 区间DP(初步了解)
区间动态规划问题一般都是考虑.对于每段区间,他们的最优值都 是由几段更小区间的最优值得到,是分治思想的一种应用,将一个区间 问题不断划分更小的区间直至一个元素组成的区间,枚举他们的组合 .求合并后的 ...
- [IOI1999]花店橱窗布置(DP路径记录)
题目:[IOI1999]花店橱窗布置 问题编号:496 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V ...
- UVA11388 GCD LCM1 2 -1
题目: 给你两个数G和L,求a和b,他们的最大公约数为G和最小公倍数为L,输出a最小时的a和b.如果不存在在输出-1. Sample Input 2 1 2 3 4 Output for Samp ...
- Android--------- SD卡存储文件报错解决
##文件找不到 1.没有写write权限 2.没有判断文件夹是否存在,不存在则创建
- asp.net mvc 删除栏目、栏目下又有子栏目的处理方式
- Mysql学习(慕课学习笔记7)修改数据表(下)
添加主键约束 ALTER TABLE tb1_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,…….) ...