[Leetcode] Z-type conversion

Source: Internet
Author: User

Subject content:

Sort the string "paypalishiring" in Z-shaped form into the given number of rows: p a h na p l s I Gy I R, then read the characters from left to right, row by row: "pahnaplsiigyir" implements a function to convert the specified number of rows of a string: String convert (string S, int numrows); Example 1: input: S = "paypalishiring", numrows = 3 output: "pahnaplsiigyir" Example 2: input: S = "paypalishiring", numrows = 4 output: "pinalsigyahrpi"

  

Ideas:

Find the regular question, the content of each letter in the string does not matter, find the rule of the lower Mark, take the second example above as an example, the subscript arrangement is like this

 /*        * 0     6     12        * 1   5 7  11 13        * 2  4  8 10  14        * 3     9             * */

  

It can be seen that, except for the oblique side, each row is an arithmetic difference series, such as, 12

The oblique side is the number in the vertical bar plus a fixed value. For example, the second row is 1 + 4 = 5, 7 + 4 = 11.

Then we can sum up the law, the difference of the arithmetic difference series is numrows + numRows-2, that is, 2 * numRows-2, is actually the number of adjacent numbers in the same row

Then, the fixed value to be added in the middle of each row is the difference of the first arithmetic difference series in turn-2, because there are two fewer numbers at each two number interval in the next row, for example, the fixed value 4 in the second row, the fixed value of the third row is 4-2 = 2.

The code is implemented later, and I believe it is not hard to write it.

If (numrows> S. length () | numrows = 1) return s; int length = S. length (); int COUNT = numrows * 2-2; int next = 0; string result = ""; for (INT I = 0; I <numrows; I ++) {for (Int J = I; j <length;) {result = Result + S. charat (j); next = J + count-2 * I; if (I! = 0 & I! = NumRows-1 & next <length) {// The number in the first row and the last row in the vertical bar plus the fixed value and the number are equal to the next value in the arithmetic difference series. // no matter whether the two rows are result = Result + S. charat (next);} J = J + count;} return result;

  

This is not the optimal solution. You can see if it can be optimized.

 

[Leetcode] Z-type conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.