LeetCode [5--longest palindrome substring] LeetCode [6--Z glyph transformation]

The longest palindrome substring

Title description

Given a string s, find the longest palindrome substring in s. You can assume that the maximum length of s is 1000.
Insert picture description here

Problem-solving ideas

You can use a sliding window like the longest substring without duplication, except that the right border of this window moves to the right, and the left border moves from the subscript of the right border to the left.
You also need a two-dimensional array to record whether the string recorded in the current window is a palindrome or not,
and you need a variable to record the length of the palindrome. Compare the largest palindrome string
Example: baccab

  1. First define the right border right, left border left = right; starting from the subscript of the right border, how to determine whether the current window is a palindrome? The first step: judging whether the two ends are equal str[i] == str[j], the second step: judging whether the string inside the window except the two ends is a palindrome: s[i+1][j-1]if the length is less than 3 right-left <= 2, and the two ends are the same The two ends must be palindromes . After the above judgment, it can be explained that the current interval is a palindrome string, so mark the i ~ j interval array[i][j] = true;and record the palindrome string and calculate the length. If the length of the interval is greater than the length of the previously recorded palindrome string, then record right-left> length . res = susbtr (left, right-left + 1); The length of right-left +1 is intercepted from the left boundary. length = right-left;
  2. For baccab, first str [right] = b, str [left] = str [right] = b, the two ends are the same, and because the length is less than 3, b is a palindrome string, and the array[i][j] =true;length is longer than the length record
  3. Then right comes to the position of a, left also comes to the position of a, and starts to go left, go to b, judge that the two ends are different, and go directly to the next loop
  4. Right comes to the position of c again, and so on ...

Code

class Solution {
public:
    string longestPalindrome(string s) {
        int n = s.size();
        //记录字符串是否为回文字符串
        vector<vector<bool>> array (n,vector<bool>(n));
        string res = "";//记录结果返回
        int length = 0; //记录长度,比较出最长的回文子串
        if(n == 0)
            return s;
        if(n == 1)
            return s;
        //如果是两个字符,则返回第一个字符
        res = s[0]; 
        //外层循环右边界
        for(int right = 0;right<n;++right)
        {
            //内层循环左边界
            for(int left = right;left>=0;--left)
            {
                //判断是否为回文字串
                if(s[left] == s[right] //两头相等
                   && (right-left<=2 //长度小于等于3,并且两头相等比为回文串
                   ||  array[left+1][right-1]) //去掉两头,中间也是回文串才是回文串
                )
                {
                    //标记left~right该区间为回文串
                    array[left][right] = true;
                    if(right-left>length)  //如果回文串长度大于之前记录的长度,则记录该串
                    {
                        res = s.substr(left,right-left+1);
                        length = right-left; //记录新长度
                    }  
                }
            }
        }
        return res;
    }
};

Zigzag transformation

Title description

Arrange a given character string in a zigzag pattern from top to bottom and from left to right according to the given number of lines.

For example, when the input string is "LEETCODEISHIRING" and the number of rows is 3, the arrangement is as follows:
Insert picture description here
Insert picture description here

Problem-solving ideas

Insert picture description here
A total of four lines numbers = 4
to find the law,

  1. Line 0, 0-6-12, with an interval of 6
  2. Line 1, 1-5-7-11-13, interval is 4-2-4-2 odd lines
    step-2*1(行)-2*1(行)-step-2*1(行)-2*1(行)
  3. Line 2, 2-4-8-10-14, interval is 2-4-2-4 Even lines
    step-2*2(行)-2*2(行)-step-2*2(行)-2*2(行)
  4. Line 3, 3-9-15, with an interval of 6

The first line and the last line, the step = 2*numbers-2
middle is the subscript spacing of the middle layer is always step-2*行数,2*行数alternating

Code

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows == 1)  //如果只有一行数据直接返回
            return s;
        int step = numRows*2 - 2;
        string res = "";
        int index = 0; //记录每行元素的下标
        int add = 0; //中间行间隔

        for(int i = 0; i<numRows;++i)
        {
            index = i; //标记行数
            add = 2*i ; //出去第0行和numRows-1行中间行的间隔
            while(index<s.size()) //元素下标大于总个数要换行
            {
                res+=s[index];
                add = step - add ; //变换间隔,
                index += (i == 0 || i == numRows-1) ? step : add; //每行每个元素的下标都在变
            }
        }
        return res;
    }
};
Published 253 original articles · praised 41 · 40,000+ views

Guess you like

Origin blog.csdn.net/liuyuchen282828/article/details/104553525
Recommended