zoukankan      html  css  js  c++  java
  • 【LeetCode】5. 最长回文子串

    引入

    题目描述

    给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

    示例 1:

    输入: "babad"
    输出: "bab"
    注意: "aba" 也是一个有效答案。

    示例 2:

    输入: "cbbd"
    输出: "bb"

    题解思路

    代码

    1. 思路一,C版
    char * longestPalindrome(char * s){
        int left = 0;
        int right = 0;    
        int maxLength = 0;      //回文子串最大长度
        int startIndex = 0;     //最长回文子串开始位置
        int index = 0;
    
        while(s[index]){
            right=index;
            left=index-1;
        
            //从当前字符开始往右读取连续重复字符(连续重复字符必定能构成回文子串,也必定是回文子串的一部分)
            //如"abcccd" 中从索引1开始的连续重复字符是"b",从索引2开始连续重复字符是'ccc'
            while(s[right]==s[index]){
                right++;
            }
    
            //定位下一个子串的中心
            index = right;
            
            //以连续重复字符为中心,往左右延展,判断当前子串是否为回文子串
            while(left >= 0 && s[right] && s[left]==s[right]){
                left--;
                right++;
            }
            
            //记录回文子串的最大长度和起始索引
            if(right-left-1>maxLength){
                startIndex = left+1;
                maxLength = right-left-1;
            }
         
        }
        
        //返回回文子串
        char* returnStr = (char*)malloc(maxLength+1);
        returnStr[maxLength]='';
        for(int i=0;i<maxLength;i++){
            returnStr[i]=s[startIndex+i];
        }
        return returnStr;
        
    }
    
    1. 思路二,java版
    class Solution {
        public String longestPalindrome(String s) {
            if (s == null || s.length() == 0) {
                return "";
            }
    //         保存起始位置,测试了用数组似乎能比全局变量稍快一点
            int[] range = new int[2];
            char[] str = s.toCharArray();
            for (int i = 0; i < s.length(); i++) {
    //             把回文看成中间的部分全是同一字符,左右部分相对称
    //             找到下一个与当前字符不同的字符
                i = findLongest(str, i, range);
            }
            return s.substring(range[0], range[1] + 1);
        }
        
        public static int findLongest(char[] str, int low, int[] range) {
    //         查找中间部分
            int high = low;
            while (high < str.length - 1 && str[high + 1] == str[low]) {
                high++;
            }
    //         定位中间部分的最后一个字符
            int ans = high;
    //         从中间向左右扩散
            while (low > 0 && high < str.length - 1 && str[low - 1] == str[high + 1]) {
                low--;
                high++;
            }
    //         记录最大长度
            if (high - low > range[1] - range[0]) {
                range[0] = low;
                range[1] = high;
            }
            return ans;
        }
    }
    
  • 相关阅读:
    用例图解析
    A B C D类网络地址
    B-树特征
    常用的中间代码
    UML图
    关于文件索引的一道习题
    数据流图的一些原则
    系统总线
    各种排序方法的时间复杂度、空间复杂度和稳定性统计表
    模拟银行自助终端系统
  • 原文地址:https://www.cnblogs.com/melodyjerry/p/12929347.html
Copyright © 2011-2022 走看看