LeetCode刷题第三天——5. Longest Palindromic Substring最长回文子串6zigzag

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

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"
这个博客说的很好,很优秀,解法全面,有空应该看下
https://www.cnblogs.com/grandyang/p/4464476.html

解法:
主要需要注意的就是:一般根据左右计算距离的时候,不要被左右这个字眼迷惑,真正理解在实际中的位置含义,尤其是这种左右变量本身也在
循环中迭代,数值不断变化,有可能出问题

#include <iostream>
#include <algorithm>
#include<string>
using namespace std;

class Solution
{
public:
    string longestPalindorme(string s)
    {
        if(s.size()<2) return s;
        int n=s.size(),maxLen=0,start=0;
        //ATTENTION:this range should be n-1,because this is i+1 at below,if <n out of range
        for(int i=0;i<n-1;++i)
        {
            //为了对比奇数和偶数回文
            SearchPalindorm(s,i,i,start,maxLen);
            SearchPalindorm(s,i,i+1,start,maxLen);

        }
         cout<<"start:"<<start<<"maxLen:"<<maxLen<<endl;
        return s.substr(start,maxLen);

    }

    void SearchPalindorm(string s,int left,int right,int& start,int& maxLen)
    {
        while(left>=0&&right<s.size()&&s[left]==s[right])
        {
            --left;
            ++right;
        }
        //这里真的很反套路:本身看到left和right都能意识到right-left+1才是长度,但这里只有当left和right不符合情况的时候才会退出循环,即此时的left和right都是被多加和间了一次的,所以真实情况是(right-1)-(left+1)+1最后拆开就是下面的情况
        if(maxLen<right-left-1)
        {
            start=left+1;
            maxLen=right-left-1;
        }
        

    }
};


int main()
{
    Solution s;
    string si,so;
    //cin>>si;
    si="abbad";
    so=s.longestPalindorme(si);

    cout<<so<<endl;
    return 0;
}

6. ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

P     I    N
A   L S  I G
Y A   H R
P     I
#coding=utf-8
class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        length = len(s)  #总共有多少元素
        step = 2*numRows-2      #有几列, 2n-2 = 0+(n-1)+(n-2)+1
        
        if s == "":      #如果空字符串,返回空字符串""
            return ""
        if length<numRows or numRows ==1 :       #如果只有一行,则直接返回原字符串
            return s
        result = s[::step] #这是第一行,他通过切片的步进step来取值,因为每个元素都是间隔一个step
                          #这题里面,首行和尾行是特殊的,所以要区别对待
        for i in range(1,numRows-1):  #开始迭代除了第一行和最后一行之外的中间所有行
            for j in range(i,length,step): #这里关键的地方,他将图形在纵向上进行切割,以每个step为标准开头,进行迭代
                result += s[j]        #在每次切割后,进行添加的第一个元素,应该是每一行的开头的元素加上某倍数的step
                if j+(step-i*2) < length:  #如果斜线上的元素是在总长度以内的
                    result += s[j+step-i*2] #那么,就添加此元素
        result += s[numRows-1::step] #最后一行开始迭代        
        print (result)
        return result
 

猜你喜欢

转载自www.cnblogs.com/Marigolci/p/10992189.html