Longest Palindromic Substring——LeetCode
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
题目大意:给一个字符串S,存在唯一的最长的回文子串,求这个回文子串。
解题思路:首先这个题有O(n)的解,是在http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html这里有详细的解释,本文的算法是O(n^2)的,中规中矩的做法。回文有两种,abba和aba,本文分别对子串以i为中心进行检查偶数和奇数哪个长,同时记录长度,下标,奇偶等信息。
Talk is cheap>>
public String longestPalindrome(String s) {
if (s == null || s.length() <= 1) {
return s;
}
int max_len = 0, pos = 0;
boolean odd = false;
for (int i = 0; i < s.length(); i++) {
int forward = i - 1;
int backward = i + 1;
int len = 0;
while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
forward--;
backward++;
len++;
}
if (max_len < (len * 2 + 1)) {
max_len = len * 2 + 1;
odd = true;
pos = i;
}
len = 0;
forward = i;
backward = i + 1;
while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
forward--;
backward++;
len++;
}
if (max_len < len * 2) {
max_len = len * 2;
odd = false;
pos = i;
}
}
if (odd) {
return s.substring(pos - (max_len - 1) / 2, pos + (max_len - 1) / 2 + 1);
}
return s.substring(pos - max_len / 2 + 1, pos + max_len / 2 + 1);
}
Longest Palindromic Substring——LeetCode的更多相关文章
- Longest Palindromic Substring -LeetCode
题目 Given a string s,find the longest palindromic substring in S.You may assume that the maximum len ...
- Longest Palindromic Substring leetcode java
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
随机推荐
- Flash上传组件之SWFUpload文件上传
一.什么是SWFUpload? SWFUpload是一个客户端文件上传工具,最初由Vinterwebb.se开发,它通过整合Flash与JavaScript技术为WEB开发者提供了一个具有丰富功能继而 ...
- iPad横竖屏代码适配
你可能非常了解用不同的方式去适配不同尺寸的iPhone屏幕,在适配iPhone屏幕时你需要考虑的只是屏幕大小变化带来的UI元素间隔的变化,但是在iPad上主要针对的是横竖屏下完全不同的UI元素的布局, ...
- Android(java)学习笔记243:多媒体之视频播放器
1.这里我们还是利用案例演示视频播放器的使用: (1)首先,我们看看布局文件activity_main.xml,如下: <RelativeLayout xmlns:android="h ...
- Oracle数据库用户数据完整备份与恢复
使用PLSQL-Developer工具可以快速便捷地完成Oracle数据库用户.表的备份恢复. Oracle数据库用户数据完整备份与恢复 1. 备份 1.1 PL/SQL->工具->导 ...
- python学习之成员信息增删改查
主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env python# coding=utf8# ...
- surfaceView和Camera配合进行摄像头的预览
首先是Camera类,在5.0上已经被废弃,不推荐使用但还是支持的.最新的是Camera2类,我还没有研究,据说用法完全不一样,反正也是一个坑跳入另外一个坑. 上面是简介和需要的权限,记得加上权限. ...
- COGS 445. [HAOI2010]最长公共子序列
#include<iostream> #include<cstdio> #include<cstring> #define mod 100000000 #defin ...
- 如何成为一名优秀的web前端工程师(转给自己,共勉)
来源:王子墨的博客 程序设计之道无远弗届,御晨风而返.———— 杰佛瑞 · 詹姆士 我所遇到的前端程序员分两种: 第一种一直在问:如何学习前端? 第二种总说:前端很简单,就那么一点东西. 我从没有听到 ...
- Gradle命令详解与导入第三方包
Android Studio + Gradle的组合用起来非常方便,很多第三方开源项目也早都迁移到了Studio,为此今天就来介绍下查看.编译并导入第三方开源项目的方法. Sublime + Term ...
- java中判断两个字符串是否相等的问题
我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...