palindromic java_Longest Palindromic Substring leetcode java

题目:

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.

题解:

第一种方法就是挨个检查,维护全局最长,时间复杂度为O(n3),会TLE。

代码如下:

1 public String longestPalindrome(String s) {

2

3     int maxPalinLength = 0;

4     String longestPalindrome = null;

5     int length = s.length();

6

7     //check all possible sub strings8     for (int i = 0; i 

9         for (int j = i + 1; j 

10             int len = j - i;

11             String curr = s.substring(i, j + 1);

12             if (isPalindrome(curr)) {

13                 if (len > maxPalinLength) {

14                     longestPalindrome = curr;

15                     maxPalinLength = len;

16                 }

17             }

18         }

19     }

20

21     return longestPalindrome;

22 }

23

24 public boolean isPalindrome(String s) {

25

26     for (int i = 0; i 

27         if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {

28             return false;

29         }

30     }

31

32     return true;

33 }

第二种方法“是对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙,例如aba是回文,abba也是回文,这两种情况要分情况考虑)往两边同时进

行扫描,直到不是回文串为止。假设字符串的长度为n,那么中心的个数为2*n-1(字符作为中心有n个,间隙有n-1个)。对于每个中心往两边扫描的复杂

度为O(n),所以时间复杂度为O((2*n-1)*n)=O(n^2),空间复杂度为O(1)。”引自Code ganker(http://codeganker.blogspot.com/2014/02/longest-palindromic-substring-leetcode.html)

代码如下:

1     public String longestPalindrome(String s) {

2         if (s.isEmpty()||s==null||s.length() == 1)

3             return s;

4

5         String longest = s.substring(0, 1);

6         for (int i = 0; i 

7             //get longest palindrome with center of i8             String tmp = helper(s, i, i);

9

10             if (tmp.length() > longest.length())

11                 longest = tmp;

12

13             //get longest palindrome with center of i, i+114             tmp = helper(s, i, i + 1);

15             if (tmp.length() > longest.length())

16                 longest = tmp;

17         }

18

19         return longest;

20     }

21

22     //Given a center, either one letter or two letter,23 //Find longest palindrome24     public String helper(String s, int begin, int end) {

25         while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {

26             begin--;

27             end++;

28         }

29         return s.substring(begin + 1, end);

30     }

Reference:

http://www.programcreek.com/2013/12/leetcode-solution-of-longest-palindromic-substring-java/

http://codeganker.blogspot.com/2014/02/longest-palindromic-substring-leetcode.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值