Font conversion algorithm problem --Z

Title Description

The number of a given string according to the given row, to down, left to right from the Z-shaped arrangement.

Example, the input string is the number of rows "LEETCODEISHIRING" is 3, arranged as follows:

L   C   I   R

E T O E S I I G

E   D   H   N

After that, you need the output from left to right read line by line, produce a new string, such as: "LCIRETOESIIGEDHN".

You will realize this string conversion function specified number of lines:

string convert(string s, int numRows);

Example 1:

输入: s = "LEETCODEISHIRING", numRows = 3

Output: "LCIRETOESIIGEDHN"

Example 2:

输入: s = "LEETCODEISHIRING", numRows = 4

Output: "LDREOEIIECIHNTSG"

Explanation:

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

Source: stay button (LeetCode)

Difficulty: Moderate

Links: https://leetcode-cn.com/problems/zigzag-conversion

answer

My problem solution

My idea: depending on the length and number of strings to calculate the total number of columns, and then create a two-dimensional array of characters, each character will turn to traverse the string on the corresponding position on the final traverse the two-dimensional array of characters draw new string.

Here to talk about problems encountered in the process of it!

The question: how many columns to calculate, due to the length of the string and the number of lines is not fixed, so the number of columns in the calculation too much trouble (in fact, not much trouble, figured on the list, beginning miscalculated, behind various subscript cross-border mentality have blown up)

Second problem: calculating a position of each character is located, assumed that a two-dimensional array of characters is strs [row] [colume], from strs [0] [0] Start into the first character, after

1. Each row increment into character

2. When the maximum row, each row while decreasing increments into the character colume

3. When the row reaches a minimum value of 0, 1 and 2, the operation is repeated until all characters into

Official explanations

Method One: Sort left
thinking

By iterating the string from left to right, we can easily determine which character within a line of Z-shaped pattern.

algorithm

We can use the min (numRows, len (s)) a Z-shaped pattern list to represent the non-blank line.

From left to right iterations ss, each character will be added to the appropriate line. You can use the current row and the current direction for these two variables to track the appropriate row.

Only when we move to move up or down the top row to bottom row, the current direction of change will occur.

public String convert(String s, int numRows) {

    if (numRows == 1) return s;

    List<StringBuilder> rows = new ArrayList<>();
    for (int i = 0; i < Math.min(numRows, s.length()); i++)
        rows.add(new StringBuilder());

    int curRow = 0;
    boolean goingDown = false;

    for (char c : s.toCharArray()) {
        rows.get(curRow).append(c);
        if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
        curRow += goingDown ? 1 : -1;
    }

    StringBuilder ret = new StringBuilder();
    for (StringBuilder row : rows) ret.append(row);
    return ret.toString();
}

the complexity:

Time complexity: O (n)

Space complexity: O (n)

Method Two: Press the line access
ideas

Z-shaped pattern in the same order and access strings read line by line.

algorithm

First-line access to all of the characters 0, followed by access line 1, and line 2, and so on ...

For all integers k,

In row 0 character located at index k (2⋅numRows-2);
lines numRows 1-characters located at index k (2⋅numRows-2) + numRows- 1;
row i in the interior of the character at index k (2⋅numRows-2) + i and (k + 1) (2⋅numRows- 2) -i place;

public String convert(String s, int numRows) {

    if (numRows == 1) return s;

    StringBuilder ret = new StringBuilder();
    int n = s.length();
    int cycleLen = 2 * numRows - 2;

    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j + i < n; j += cycleLen) {
            ret.append(s.charAt(j + i));
            if (i != 0 && i != numRows - 1 && j + cycleLen - i < n)
                ret.append(s.charAt(j + cycleLen - i));
        }
    }
    return ret.toString();
}

the complexity:

Time complexity: O (n)

Space complexity: O (n)

Guess you like

Origin www.cnblogs.com/hao-yu/p/11672629.html
Recommended