Leetcode "6". ZigZag Conversion--thought diagram and Java implementation

Source: Internet
Author: User

ZigZag Conversion

first, the topic is as follows:The string "PAYPALISHIRING" is written in a zigzag the pattern on a given number of rows type this: (The want to display the this pattern in a fixed Font for better legibility)

P   A   H   NA p L S i i GY   i   
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" .

The main idea is to rearrange the given string as the "Z" zigzag above.

second, the idea

to partition the graphic.


Figure I, partition map

from the above we can be very clear based on the index of the given string to find its position in a "Z" type, is in vertical lines or oblique row, is in the first row. Clear thinking, combined with the following code, comments, you can quickly understand the idea.  

Third, Java implementation

public class Solution {public string convert (String s, int numrows) {int SL = s.length (); if (numrows<=1| |                Sl<=numrows) return s;  int N = (int) Math.ceil ((double) sl/(2*numrows-2)); 1.        Calculate the partition size, the last area may be full or may be dissatisfied, so rounding up int index1=0, index2=0;                              int NN = 2*numrows-2; 2. Find out how many elements are in a partition.                Vertical lines is a numrows, diagonal line minus two elements stringbuffer sb = new StringBuffer (); for (int iR = 1; ir<=numrows; ir++)//3. Scan output by row {for (int jN = 1; jn<=n; jn++)//4. Scan different partitions of the IR row {/ /4.1.                                                      Index1 for the first JN block of the column IR row vertical value Index index1 = (jN-1) *nn + IR;                        if (INDEX1&LT;=SL) {sb.append (S.charat (index1-1)); }//4.2.          Index2 for the first JN block of the index of the value of the IR line, oblique line to turn tail two if ((ir!=1) && (ir!=numrows))         {index2 = (jN-1) *nn + 2*numrows-ir;                                  if (INDEX2&LT;=SL) {sb.append (S.charat (index2-1));    }}}} return Sb.tostring (); }}



Leetcode "6". ZigZag Conversion--thought diagram and Java implementation

Related Article

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.