LeetCode 6 snake-like matrix, a simple simulation questions


The meaning of problems


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)

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


translation


Given a string, it becomes serpentine output. The snake-like abstract concept, we need to combine sample to understand.


Sample


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

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


analysis


This problem meaning of the questions a bit Kichiku, meaning that a given string first arranged in a serpentine good, and then press the line in order to read, and finally a new string.

While the difficulty of this question to the LeetCode is Medium , but in fact it is quite simple. And Manchester algorithm on a question than it is regarded as a very simple. LeetCode 5 Manchester algorithm quickly determine the palindromic sequence

This question will tell us the string and the number of lines snake twisted strings arranged in a serpentine shape. This does not have any algorithm or data structure, only a matter of realization that Italy is called simulation questions. Obviously today this problem is a simulation questions. The only problem is the difficulty of the simulation code, to achieve some of the more complex functions, in fact, it is the test of engineering capabilities.

The snake-like arrangement is also very simple, because we only output the last row connection. So we can ignore the location information column, only the line of interest placed just fine. Because the number of lines is limited, for each row, we can put a string in the current row so far with a string of records, the last line in the order of the results of all the lines connected to the fine together. Through observation, we can easily find, placed in a row is a cycle regularly. One cycle is 2 * rowJum - 2, from 0 to increment the rowNum - 1, and then decreases to 1.

After the discovery of the law, then it is not difficult to write code.

def convert(text, numRows):
    # 记录每一行结果的dict
    lines = {}
    if numRows == 0:
        return text

    for i in range(len(text)):
        # 计算应该放在哪一行
        idx = i % (2 * numRows - 2)
        # 判断是在递增区间还是递减区间
        if idx >= numRows:
            idx = 2 * numRows - 2 - idx

        line = lines.get(idx, "")
        lines[numRows] = line + text[i]

    result = ""
    # 拼接答案
    for i in range(numRows):
        result += lines[i]
    return result

The above code is very simple, but hidden in a place that can be optimized.

That the use of dict, dict queries require overhead. In fact, we can replace the array , because we have identified a number of rows, so the length of the array is fixed. After such optimization, time efficiency will be a little higher. But the difference is not great, the code hold, I think we should be able to want to understand.

Today's article on here, if that be harvested, tap a concern it.

Guess you like

Origin www.cnblogs.com/techflow/p/12185531.html