给定输入字符串,要求判断任意子字符串是否对称。

基本思路就是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的更多相关文章

  1. Palindrome Partitioning 解答

    Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...

  2. PALIN - The Next Palindrome 对称的数

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  3. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  5. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  6. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  7. [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 ...

  8. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. Ext.window的close的问题

    以前每次都是用的hide,关闭后隐藏窗体,下一次点击再打开,这种方法在我的随笔里面有,可是现在遇到一个问题,我的窗体里面有个formpanel,formpanel每一项都有一个默认值,意思就是修改的时 ...

  2. c语言编程之sglib库的简单使用

    说实话自从大学毕业后已经很久没有用c语言写过程序了,一般都是使用c++,c++的stl和boost等,这些代码库大大简化了我们的编程复杂度.由于最近某种原因在次开始用c写程序.我是个比较懒的人,比较喜 ...

  3. python scrapy 基础

    scrapy是用python写的一个库,使用它可以方便的抓取网页. 主页地址http://scrapy.org/ 文档 http://doc.scrapy.org/en/latest/index.ht ...

  4. Linux RAR 安装和使用详细说明

    描述:Linux默认自带ZIP压缩,最大支持4GB压缩,RAR的压缩比大于4GB.  流程:下载 >安装 > 使用  ----------------------------------- ...

  5. [RxJS] Combining Streams with CombineLatest

    Two streams often need to work together to produce the values you’ll need. This lesson shows how to ...

  6. IOS Layer的使用

    CALayer(层)是屏幕上的一个矩形区域,在每一个UIView中都包含一个根CALayer,在UIView上的所有视觉效果都是在这个Layer上进行的. CALayer外形特征主要包括: 1.层的大 ...

  7. 为iPhone6设计自适应布局(二)

    Size Classes 自适应布局的布局约束自然是好东西,但也不是万能的,有时候我们也需要使用最基本的布局,所以使用size classes将它们两者结合起来才能碰撞出更有激情的火花. 引用我上篇译 ...

  8. Win32多线程编程(1) — 基础概念篇

      内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Serv ...

  9. jquery压缩图片插件

    imageCompress 只有图片压缩功能,比较简单jquery.imageCompress.js 使用说明: el:为上传框 quality:压缩图片质量,单位为% onloadStart:读取图 ...

  10. Mysql存储引擎概念特点介绍及不同业务场景选用依据

    目录 MySQL引擎概述 1 MySAM引擎介绍 2 什么是InnoDB引擎? 3 生产环境中如何批量更改MySQL引擎 4 有关MySQL引擎常见企业面试题 MySQL引擎概述 Mysql表存储结构 ...