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

基本思路就是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. ACM2136

    /* Problem Description Everybody knows any number can be combined by the prime number. Now, your tas ...

  2. 面试题32.从1到n整数中1出现的次数

    题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从 1到12这些整数中包含1的数字中1,10,11和12,1一共出现了5次 本题可以直接变量1到n的n个数然后分别计 ...

  3. 关于bootstrap--表单(按钮<button>效果、大小、禁用)

    1.各种标签实现按钮效果: <button class="btn btn-default" type="button">button标签按钮< ...

  4. 解决Fetching android sdk component information加载过久问题

    安装完成后,如果直接启动,Android Studio会去获取 android sdk 组件信息,这个过程相当慢,还经常加载失败,导致Android Studio启动不起开.解决办法就是不去获取and ...

  5. _js day11

  6. jquery之批量上传图片

    //var btn; /** * * 获取当前时间 */ ==================================js=================================== ...

  7. ORA-02069: global_names parameter must be set to TRUE for this operation

    原因:在对远程表增删改操作的时候,调用了本地函数.  比如:insert into trans_load_rate@DC values(rate_s(1)); trans_load_rate是DC库的 ...

  8. (转)ie -ms-interpolation-mode: bicubic 属性详解

    ie -ms-interpolation-mode: bicubic 属性详解  img { -ms-interpolation-mode: bicubic; } 这个在做实时缩放图片.缩略图的时候用 ...

  9. c#串口编程和单片机通信重大发现

    1.遇到问题时看看这里 //每次响应中断结束后清空缓存,防止在显示关闭时,打开后又一次性出现 serialPort1.DiscardInBuffer();

  10. vc2015 编译libcurl带openssl

    1.先编译zlib下载地址 http://zlib.net/ 我这边vc2015编译需要配置环境变量,不知道是装了wdk的原因还是多个vc版本的原因 设置环境变量lib和include路径 INCLU ...