Get the App
SLTechnology News&Howtos  ›  Development  › 

How to calculate the substring of data structure

Shulou Source: shulou.com Published: 2022-06-03 16:42:03 09月19日 Update

Today, I will talk to you about how to calculate the substrings of the data structure, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

I. explanation

A palindrome string is a string that is the same for both positive and negative reading.

Given a string s, find the longest palindromic substring in s. You can assume that the maximum length of s is 1000.

Example 1:

Enter: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example 2:

Enter: "cbbd"

Output: "bb"

II. Solution reference

1. Swift language

Class LongestPalindromicSubstring {func longestPalindrome (_ s: String)-> String {guard s.characters.count > 1 else {return s} var sChars = [Character] (s.characters) let len = sChars.count var maxLen = 1 var maxStart = 0 var isPalin = Array (repeating: Array (repeating: false, count: len) Count: len) / / set palindrome whose len is 1 for i in 0...len-1 {isPalin [I] [I] = true} / / set palindrome whose len is 2 for i in 0...len-2 {if sChars [I] = = sChars [I + 1] {isPalin [I] [I + 1] ] = true maxLen = 2 maxStart = I}} if len > = 3 {for length in 3...len {for i in 0...len-length {if sChars [I] = = sChars [I + length-1] & & isPalin [I + 1] [I + length- 2] {isPalin [I] [I + length-1] = true maxLen = length maxStart = I} return String (Schars [maxStart.. maxStart + maxLen-1])}}

2. JavaScript language

/ * * @ param {string} s * @ return {string} * / return the Longest Palindromic Substring of sfunction Manacher (s) {var str ='* #', dp = [], maxn = 0, idx = 0; for (var I = 0, len = s.resume; I

< len; i++) str += s[i] + '#'; for (var i = 1, len = str.length; i < len; i++) { if (maxn >

I) dp [I] = Math.min (dp [2 * idx-I], maxn-I); else dp [I] = 1; while (str [I-dp [I]] = = str [I + dp [I]) DP [I] + +; if (dp [I] + I > maxn) maxn = dp [I] +, idx = I;} var ans = 0, pos; for (var I = 1; I)

< len; i++) { if (dp[i] >

Ans) ans = dp [I], pos = I;} var f = str [pos] ='#', tmp = f?': str [pos], startPos = f? Pos + 1: pos + 2, endPos = f? Dp [pos]-3 + startPos: dp [pos]-4 + startPos; for (var I = startPos; I = 0 and r

< n and s[l] == s[r]: l -= 1 r += 1 if r - l - 2 >

Right-left: left = l + 1 right = r-1 l = I r = I + 1 while l > = 0 and r

< n and s[l] == s[r]: l -= 1 r += 1 if r - l - 2 >

Right-left: left = l + 1 right = r-1 return s [left:right + 1]

4. Java language

Public String longestPalindrome (String s) {if (s = = null | | s.length ()

< 1) return ""; int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(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);} private int expandAroundCenter (String s, int left, int right) {int L = left, R = right; while (L > = 0 & R

< s.length() && s.charAt(L) == s.charAt(R)) { L--; R++; } return R - L - 1;} 5. C++ 语言 #include #include #include #include using namespace std;string findPalindrome(string s, int left, int right){ int n = s.size(); int l = left; int r = right; while (left>

= 0 & & right=0 & & right len) {len = right-left-1; start = left+1;}} / / The following solution is better than previous solution.//Because it remove the sub-string return in findPalindrome (). String longestPalindrome_recursive_way2 (string s) {int n = s.size (); if (n matrix (n, vector (n); / / Dynamic Programming / / 1) if I = = j, then matrix [I] [j] = true / / 2) if I! = j, then matrix [I] [j] = (s [I] = s [j] & & matrix [iTun1] [jmur1]) for (int iposure 1; I > = 0; iMube -) {for (int jafi; j 3, then, check s [I] = = s [j] & matrix [iLiv 1] [JLV 1] if (iLim j | | (s [I] = = SJ] & & (JLI)

Tags: Palindromes language longest content subscript data data structure structure maximum center point character string example length input output valid radius right scheme Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Microsoft Shulou Tech Info Shulou Information