首页 > 其他分享 >lc_模拟_回文串_0921

lc_模拟_回文串_0921

时间:2022-09-21 22:01:17浏览次数:87  
标签:head ListNode lc int 0921 next return curr 回文

lc5 最长回文子串

1 动态规划

class Solution {
    public String longestPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }
        int maxLen = 1, begin = 0;
        boolean[][] dp = new boolean[len][len];
        for (int i = 0; i < len; i++){
            dp[i][i] = true;
        }
        char[] chars = s.toCharArray();
        for (int L = 2; L <= len; L++) {
            for (int i = 0; i < len; i++) {
                int j = L + i - 1;
                if (j >= len) {
                    break;
                }
                if (chars[i] != chars[j]) {
                    dp[i][j] = false;
                } else {
                    if (j - i < 3) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i+1][j-1];
                    }
                }
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, begin + maxLen);
    
    }
}

2 中心扩展算法

class Solution {
    public String longestPalindrome(String s) {
        int length = s.length();
        if (length < 2) {
            return s;
        }
        int start = 0, end = 0;
        for (int i = 0; i < length; i++) {
            int len1 = expand(s, i, i);
            int len2 = expand(s, i, i + 1);
            int len = Math.max(len1, len2);
            if (len > end - start) {
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }
        return s.substring(start, end + 1);
    }
    public int expand(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            --left;
            ++right;
        }
        return right - left - 1; 
    }
}

3 Manacher

lc

标签:
head,ListNode,lc,int,0921,next,return,curr,回文
From: https://www.cnblogs.com/beichuang/p/16717289.html

相关文章

  • 0921v-for
    <html> <head> <metacharset="utf-8"/> <title></title> <scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script> </head><body> <divi......
  • 0921v-show
    <html> <head> <metacharset="utf-8"/> <title></title> <scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script> </head><body> <divi......
  • 20220921 模拟赛
    T1彩票\(n\leq10^5\)。发现三等奖有三种情况,一等奖和二等奖却都只有一种情况,感觉很烦,考虑暴力做掉三等奖的彩票号码,直接三重循环枚举,这一部分消耗\(O(\dfrac{100^3......
  • lc-0921
    lc206反转链表classSolution{publicListNodereverseList(ListNodehead){ListNodeprev=null;ListNodecurr=head;while(cu......
  • springboot+Flink(读取kafka并存入Mysql)20220921
    1、mysql数据库test 2、kafka创建主题student  3、pom.xml<properties><java.version>1.8</java.version><project.build.sourceEncod......
  • pdfjs-dist 后端返回文件前端实现预览pdf
    pdfjs-dist锁定版本号2.2.228,别的都不太好使,各种各样的报错不锁定的时候升高版本出现pdf预览不了引用的时候 importpdfjsLibfrom'pdfjs-dist/build/pdf.js'importp......
  • 回文串
    packagebook.chapter6.demo2;importjava.util.Scanner;//回文串publicclassText8{publicstaticvoidmain(String[]args){Scannerscanner=ne......
  • LC1143 最长公共子序列
    intlongestCommonSubsequence(stringtext1,stringtext2){//dp[i][j]记录text1前i序列和text2前j序列的最长公共序列intdp[1005][1005];m......
  • LC300 LCS
    intlengthOfLIS(vector<int>&nums){intlen=nums.size();intdp[len];intlist[len];//记录序列下标vector<int>v;for(inti=0;i<len;i++){......
  • HO引擎近况20220921
    这俩月一直在忙公司的项目,好多事都要我来处理我打算在公司再买一台显示器组三显,但是公司的显卡太破了接口不够,我只好又买了一张显卡自从之前XGP被回收之后我也没有再......