Leetcode-6 ZigZag Conversion

Source: Internet
Author: User



Problem Description:

The string "Paypalishiring" is written in a zigzag the pattern on Agiven number of rows type this : (The want to display the this pattern in afixed fo NT for better legibility)

P A H N

A P L S i i G

Y I R

And then read on line : "Pahnaplsiigyir"

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

Stringconvert (string text, int nRows);

convert ("paypalishiring", 3) should return "Pahnaplsiigyir" .



Solution One: Formula method


/   *       0        8   1     7 9       2 6 (3 5) 4 12*/public Class Solution {Public    string convert (String s, int nRows) {//parameter to determine if (s = = NULL | | s.length () = = 0 | | nRows <= 1) { return s;} char[] Strarr = S.tochararray (); StringBuilder Strbuilder = new StringBuilder (); Boolean flag = True;int Temp_index = 0;for (int i = 0; i < nRows; i++) {f  lag = true;for (int j = 0; flag; j + +) {flag = false;if (i = = 0 | | i = = nRows-1)//first line and last line condition {Temp_index = 2 * J * (NRows- 1) + I;} else {if ((J & 0x1) = = 0)//even case {Temp_index = i + J * (NROWS-1);} Else{temp_index = i + 2 * (NRows-1-i) + (J/2 * 2) * (NROWS-1);}} if (Temp_index < S.length ()) {strbuilder.append (Strarr[temp_index]); flag = True;}}}        return strbuilder.tostring ();    }}



Solution Two: Enumerate all possible


public class Solution {public    String convert (String s, int nRows) {//parameter determine if (s = = NULL | | s.length () = = 0 | | nRows < ; = 1) {return s;} char[] Strarr = S.tochararray (); StringBuilder Strbuilder = new StringBuilder (); Boolean flag = True;int Temp_index = 0;for (int i = 0; i < nRows; i++) {f  lag = true;for (int j = 0; flag; j + +) {flag = false;if (i = = 0 | | i = = nRows-1)//first line and last line condition {Temp_index = 2 * J * (NRows- 1) + i;if (Temp_index < S.length ()) {strbuilder.append (Strarr[temp_index]); flag = True;}} else {Temp_index = 2 * J * (NROWS-1)-I;if ((Temp_index > 0) && (Temp_index < S.length ())) {Strbuilder.appen D (Strarr[temp_index]); flag = true;} Temp_index = 2 * J * (NROWS-1) + i;if (Temp_index < S.length ()) {strbuilder.append (Strarr[temp_index]); flag = True;}} }        return strbuilder.tostring ();}    }


Leetcode-6 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.