Convert a string into zigzag mode

Source: Internet
Author: User

Background:

Original problem Source: https://leetcode.com/problems/zigzag-conversion/

is to convert a string in zigzag format and return it. For example, the string "Abcdefghijk"

After conversion (3 lines):

A E IBDFHJC G K

Then print by line: AEIBDFHJCGK

If you are converting by 4 rows:

A GB Fhce IKD J

Printed: AGBFHCEIKDJ

Ideas:

In fact, the problem is like finding a regular problem. When converting to 3 lines, we can be in 4 units (4 = (3-1) * *. That is equal to (line number-1) * *), divide the string (take Abcdefghijk as an example ):

ABCD EFGH IJK

Then the No. 0 line that is printed after the conversion is actually the No. 0 element of each array after the split: a E I. corresponding to the original string is the No. 0, 4th, 8th.

So the law is 0,4,8,... 4*n.

then the 2nd line (the last line) printed after the conversion: Each element after printing is: C G K. In fact, it is the second element of each array after the split (subscript starting from 0) . Corresponds to the original string is: 2, 2+4,2+4*2,.. 2+4*n.

As can be seen from the above, the first line and the last line of the law is (set the number of rows for I, starting from 0): I+4*n (n=0,1,2 ...)

Other lines:

Or the above example, the first line of output is: B D F H J. Each element is the 1th and 1 elements in the segmented array. Corresponds to the original string as:

1,4-1,4+1,4*2-1,4*2+1 ...

So the rule is (set I as the number of rows): 4*n+i,4*n-i.

Code:

My code is not easy to write, and time is about 110ms, and no less time for this friend's code (107MS), so recommend the friend's code:

Source Code Origin: Https://leetcode.com/discuss/11948/my-python-solution

class solution:    #  @return  a string    def  Convert (self, s, nrows):         if nrows == 1 :            return s         d = 2*nrows -2        l =  len (s)         R =  '          for i in range (0, nrows):             MD = 2*nRows - 2 - 2*i             t = i             while t < L:                 r += s[t]                 if i != 0 and i!= nRows-1 and t+MD <  L:                     R += s[t+MD]                 t += d        return r


Convert a string into zigzag mode

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.