Leetcode6 Zigzag Conversion

Source: Internet
Author: User

Title Requirements:

The string is "PAYPALISHIRING" written with 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   NA p L S i i GY   i   R

and then read line by line:  " Pahnaplsiigyir "

    use coordinate transformation to do this problem. We now consider the nrows=4, in fact, for each row, if you do not consider the middle column, only the full-full column, then the next coordinate is the previous base on the basis of plus (NROWS-1), such as 0->6->12,1->7->13,2-> 8,3->9, therefore, for the i=0 line and the I=nrows line, directly in accordance with such a coordinate conversion read into, and for the middle row, each time according to such a law read into a character, you need to append a character, 1->5,2->4. What is the law of this coordinate transformation, for 0->6, is a nrows=4 zigzag, similarly, for < Span style= "font-size:18px" >1->5,2->4, respectively < Font-family:monospace of span style= "font-size:18px" >nrows=3, , that is, line I, the law of increasing coordinates increases 2* (nrows-1-i) on the basis of the previous coordinate.


完整代码如下:


Leetcode6 Zigzag 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.