leetcode 5. Longest Palindromic Substring
2016-04-22 08:47
381 查看
//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. public class Solution { public static void main(String[] args) { String a = "abbaabba"; String result = longestPalindrome(a); System.out.println(result); } public static String longestPalindrome(String s) { String result = ""; int length = 0; for(int i = 0;i<s.length();i++){ String temp1 = findString(s,i,i); //奇数字符串寻找回文字符串 if(length<temp1.length()){ length = temp1.length(); result = temp1; } String temp2 = findString(s,i,i+1); //偶数字符串寻找回文字符串 if(length<temp2.length()){ length = temp2.length(); result = temp2; } } System.out.println(result); return result; } //从中心向两侧扩展寻找回文字符串的函数 public static String findString(String str, int left, int right){ while(left>=0&&right<=str.length()-1&&str.charAt(left) == str.charAt(right)){ left--; right++; } return str.substring(left+1, right); } }
相关文章推荐
- 细说C#多线程那些事 - 线程同步和多线程优先级
- 【CodeForces】554A - Kyoya and Photobooks(思维)
- java 带滚动条的多行文本输入
- yii2分页
- iOS中如何在欢迎界面中动态加载广告
- Apache + Tomcat + jk配置负载均衡以及虚拟主机 实现多域名、多用户同时使用
- 实验6 在应用程序中播放音频和视频
- Android课程---远程服务器存储
- 数据结构与算法--查找与排序(二)
- 在SQL存储过程中给条件变量加上单引号
- 解决vavido “No hardware target is open” 问题
- html标签元素分类-行内元素和块级元素
- Python 利用 filter() 滤掉非回数
- asp.net mvc5 安装
- http://www.oschina.net/question/1019034_153316
- acm 2 1016 Red and Black
- 基于百度地图记录运动轨迹案例分析与实现
- 每日站立会议——20160422
- 债券、期权以及清算协议在科技公司生命周期中的作用
- iOS 中各种折线.曲线.图形绘制方法集锦(折线图.走势图.柱状图.动态曲线图.形状图.划线图)