Leetcode ZigZag Conversion C + + problem solving ideas

Source: Internet
Author: User

A difficult problem for easy, see a lot of people's problems are not trying to understand, and finally hard to think hard, just think about the same. It's too weak to find a job.

Title Description:

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 on line:"PAHNAPLSIIGYIR"

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

String convert (string text, int nRows);

convert("PAYPALISHIRING", 3)should return"PAHNAPLSIIGYIR"

n=2

0 2 4 6

1 3 5 7

N=3

0 4 8

1 3 5) 7 9

2 6 10

N=4 the way to go is:

0 6 12

1 5 7) 11 13

2 4 8) 10 14

3 9 15

The problem is to find a regular problem.

Analyze the rules of each line and then piece together.

A closer look reveals that a vertical column and a diagonal column are a period, with a period of 2n-2,n being the number of rows. A vertical column has n elements, the first row and the last row have no elements so it is n-2 elements, so the add-up period is 2n-2 elements. The distance difference between the elements on each row adjacent to the vertical column is a period, for example, when n=4, the period is 2*4-2 = 6, the element 0 and 6 are 6, the elements 1 and 7 differ by 6.

In each row in the middle, there is an oblique element in the middle of each of the two vertical columns, for example, 1 and 7 are 5,7 and 13 in the middle is 11. The middle oblique element differs from the element on the previous vertical column to a period of -2* row index. As can be understood, for example, when n=4, the element of the next period of 2 is 8, then 2 and 4 distance is 2 to 8 distance minus 4 to 8 distance, 2 to 8 distance is a period, 4 to 8 is 1+2+2+......+1, the first 1 is the element of the No. 0 row, The last 1 is the element (that is, 8) of the current row (line I), with two elements in the middle of each row, since I start with 0, so there is a total of I 2. So the distance from 2 to 4 is 2n-2-2i, if the position of 2 is J, then the position of 4 is j+2n-2-2i,8 position is j+2n-2.

The code is as follows:

stringConvertstringSintnRows) {    if(s.length () = =0|| nrows<=0)        return ""; if(NRows = =1)        returns; stringRes; intSize =2*nrows-2;  for(inti =0; i < nRows; i++)         for(intj = i; J <s.length (); j+=size) {Res+=S[j]; if(i!=0&& I! = nrows-1){                inttemp = j+size-2*i; if(Temp <s.size ()) Res+=S[temp]; }        }    returnRes; }

REF:[1]. Http://www.cnblogs.com/springfor/p/3889414.html

[2].http://blog.unieagle.net/2012/11/08/leetcode%e9%a2%98%e7%9b%ae%ef%bc%9azigzag-conversion/

Leetcode ZigZag Conversion C + + problem solving ideas

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.