[Medium] 6-Z conversion Zigzag Conversion

topic

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)

L   C   I   R
E T O E S I I G
E   D   H   N

And then read line by line: "PAHNAPLSIIGYIR"

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

Arrange a given character string according to the given number of lines, from top to bottom, from left to right.

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

L   C   I   R
E T O E S I I G
E   D   H   N

Example

输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:

L     D     R
E   O E   I I
E C   I H   N
T     S     G

Source: LeetCode
Link: https://leetcode.com/problems/zigzag-conversion

solution

Method 1: Determine the line number character by character

Problem-solving ideas

Make sure that the number of rows is numRows, the characters fall from the first row, and the subsequent character string moves downward. When it cannot be downward, it moves upward. Use nunrows character strings to store the characters of each row, and finally connect them.

Code

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows < 2) return s;
        string result[numRows];
        for(int i = 0; i < numRows; ++i) result[i] = "";
        int curr = 0, dir = 1;
        for(int i = 0; i < s.length(); ++i){
            result[curr] += s[i];
            if(curr == numRows-1) dir = -1;
            else if(curr == 0) dir = 1;
            curr += dir;
        }
        string res = "";
        for(int i = 0; i < numRows; ++i){
            res += result[i];
        }
        return res;
    }
};

Method 2: Determine characters line by line

Problem-solving ideas

For numRows row data, except numRows = 0 and numRows = 1, groupnum = 2 numRows-2 numbers can be divided into a group, a total of n groups. At this time, for the line number i, there must be the characters of index = i + t goupnum (0 <= t <= n, 0 <= index <s.length ()), except for line 0 and numRows-1 Line, each group has two characters in the line, that is, i + (t + 1) goupnum-2 i = (t + 1) * groupnum-i is also in this line, therefore, only need to output each group in turn Just the characters that fall on the line

Code

class Solution {
public:
    string convert(string s, int numRows) {
        string result = "";
        if(numRows < 2) return s;
        int group_num = numRows+numRows-2, line = 0;
        for(int i = 0; i < numRows; ++i){
            int pos = i;
            while(pos < s.length()){
                result = result+s.substr(pos, 1);
                pos += group_num;
                if(i != 0 && i != numRows-1 && pos-i*2 < s.length()){
                    result = result+s.substr(pos-i*2, 1);
                }
            }
        }
        return result;
    }
};

to sum up

  1. Since method two involves a lot of calculations, the cost is larger than the method

Guess you like

Origin www.cnblogs.com/suata/p/12711824.html