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 ...
随机推荐
- STL中,back_insert_iterator与back_inserter的区别。
1.参考http://www.cplusplus.com网站关于back_insert_iterator与back_inserter的介绍之后,我总算明白了:back_insert_iterator, ...
- Android自定义ListView的Item无法响应OnItemClick的解决办法
转: 如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也 ...
- STL中序列式容器的共性
代码如下: /* * vector_1.cpp * * Created on: 2013年8月6日 * Author: Administrator */ #include <iostream&g ...
- C# 二分查询
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- jQuery鼠标悬停图片放大显示
jQuery鼠标悬浮放于图片上之后图片放大显示的效果,即鼠标移到图片上图片突出显示,鼠标移开后恢复原来的模样,你可以在图片滚动效果中加上本特效,相信会更炫一些. <!DOCTYPE html P ...
- jQuery限制文本框只能输入正整数
//限制键盘只能按数字键.小键盘数字键.退格键 $("#txtQty").keydown(function (e) { var code = parseInt(e.keyCode) ...
- hdu-1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10219 Accepted: 4977 Des ...
- js_day13
- C# 将窗口移动到指定位置
private void button1_Click(object sender, EventArgs e) { Frm f = new Frm(); f.StartPosition = FormSt ...
- windows程序消息机制(Winform界面更新有关)--转
1. Windows程序消息机制 Windows GUI程序是基于消息机制的,有个主线程维护着消息泵.这个消息泵让windows程序生生不息. Windows程序有个消息队列,窗体上的所有消息是这个队 ...