Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80856
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 15:31
文章分类

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: Java

2015-01-17 22:50:26

题目: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.
最直观的解法,找出该字符串所有子字符串,依次判断该子字符串是否为回文字符串,之后再找出最长的子回文字符串,程序如下:

点击(此处)折叠或打开

  1. public class Solution {
  2.     public String longestPalindrome(String s) {
  3.         String tempstring = new String();
  4.         int len = 0;
  5.         int strlen = s.length();
  6.         for(int i = 0; i < strlen; i++){
  7.             for(int j = i+1; j <= strlen; j++){
  8.                 String substring = s.substring(i,j);
  9.                 if(isPalindrome(substring)){
  10.                     if(tempstring.length() < substring.length()){
  11.                         tempstring = substring;
  12.                     }
  13.                 }
  14.             }
  15.         }
  16.         return tempstring;
  17.     }
  18.     public boolean isPalindrome(String s){
  19.         char[] chararr = s.toCharArray();
  20.         int strlen = s.length();
  21.         boolean result = true;
  22.         for(int i = 0; i < strlen/2; i++){
  23.             if(chararr[i] == chararr[strlen-i-1]){
  24.                 result = true;
  25.             }
  26.             else{
  27.                 result = false;
  28.                 break;
  29.             }
  30.         }
  31.         return result;
  32.     }
  33. }
但该程序时间复杂度过高,不会被OJ接受,一个可被OJ接受的算法是,从某一个字符向两边分别进行比较,以找出以该字符为中心的回文子字符串,实现方法如下:

点击(此处)折叠或打开

  1. public class Solution {
  2.     private int resultlen = 0;
  3.     private String result = new String();
  4.     public String longestPalindrome(String s) {
  5.         char[] chararr = s.toCharArray();
  6.         int len = chararr.length;
  7.         for(int i = 0; i < len; i++){
  8.             findPalindrome(chararr,len,i,i+1);
  9.             findPalindrome(chararr,len,i,i);
  10.         }
  11.         return result;
  12.     }
  13.     private void findPalindrome(char[] myarr,int len,int left,int right){
  14.         int templen = 0;
  15.         StringBuilder sb = new StringBuilder();
  16.         while(left >= 0 && right < len && myarr[left] == myarr[right]){
  17.             left--;
  18.             right++;
  19.         }
  20.         if ((right-left-1) > resultlen) {
  21.             resultlen = right-left-1;
  22.             result = new String(myarr, left+1, resultlen);
  23.         }
  24.     }
  25. }

                                                                                                                                                                                              
                                                                                                                                                          
阅读(683) | 评论(0) | 转发(0) |
0

上一篇:LeetCode OJ 之 Add Two Numbers

下一篇:分治算法

给主人留下些什么吧!~~