Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write code to realize the longest palindromic subsequence of a string

Shulou Source: shulou.com Published: 2022-06-03 05:35:40 11月17日 Update

This article introduces the knowledge of "how to write code to achieve the longest return subsequence of a string". Many people will encounter such a dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Import java.util.Arrays;/** * @ author pxu * @ create 2021-4-7-5:57 p.m. * / public class Nc154 {public int longestPalindromeSubSeq (String s) {int n = s.length () During the for loop, the data in dp [j] represents the length of the longest palindrome sequence in the substring of s from I to j * after the end of for operation, the data in dp [j] represents the length of the longest palindrome sequence in the substring of s from 0 to j, so the final result returned by the program is the value of DP [n-1]. * / int [] dp = new int [n]; / * is filled with 1 because each character is a palindromic string of length 1 * / Arrays.fill (dp,1); for (int I = nMuth2sidi > = 0 IMel -) {/ * pre always represents the length of the longest palindrome sequence in the substring of the string s from iMel 1 to JMUI 1, so its initial value is set to 0 * / int pre=0; for (int Jubi iSidsj)

Tags: Character palindrome longest sequence length loop string representative code content foundation data more knowledge process run practical learned next reason Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker MariaDB Shulou Technology NVidia