"Leetcode-Interview algorithm classic-java Implementation" "006-zigzag Conversion (z-font conversion)"

Source: Internet
Author: User
Tags compact

"006-zigzag Conversion (z-font conversion)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

The string "Paypalishiring" is written with a zigzag pattern on a given number of rows like this: (You could want to display T His pattern in a fixed font for better legibility)
P A H N
Aplsiig
Y I R
And then read 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".

Main Topic

Enter a string and the specified number of lines to output the characters in Z-font.

Thinking of solving problems

Calculates the maximum number of columns of a character, creates a one-dimensional array based on the number of columns and rows, computes the position of each character in a Fu Zheng-dimensional array, and then makes a compact operation of the characters in a one-dimensional array, returning the result.

Code Implementation
 Public  class solution {     PublicString Convert (String s,intNRows) {if(s = =NULL|| S.length () <= NRows | | NRows = =1) {returnS }int Index= S.length ();intRowlength =0;//Calculates the length of the line, including the last line break character        intSlash = NRows-2;//A slash to remove the number of rows occupied by the end and end         while(Index>0) {//A column in a vertical shape            Index-= NRows; rowlength++;//Diagonal Number of columns             for(inti =0; I < slash &&Index>0; i++) {rowlength++;Index--; }        }Char[] result =New Char[NRows * rowlength];//Save an array of results, and the last column to save the line break         for(inti =0; i < result.length; i++) {//Initialize to a spaceResult[i] ="'; }intCurcolumn =0;//number of rows currently being processed        Index=0; while(Index< S.length ()) {//Handle Vertical line             for(inti =0; I < nRows &&Index< S.length (); i++) {Result[rowlength * i + curcolumn] = S.charat (Index);Index++; } curcolumn++;//Handle slash             for(inti = nRows-2; i >0&&Index< S.length (); i--) {Result[rowlength * i + curcolumn] = S.charat (Index); curcolumn++;Index++; }        }//System.out.println (new String (result));        //Compact manipulation of character arrays        Index=0; while(Index< S.length () && result[Index] !="') {//Find the first character position that is a space            Index++; }intNext =Index+1; while(Index< S.length ()) { while(Next < Result.length && Result[next] = ="') {//Find elements that are not spacesnext++; } result[Index] = Result[next];Index++;        next++;        } System.out.println (s); System.out.println (NewString (Result,0,Index));return NewString (Result,0,Index); }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46938327"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "006-zigzag Conversion (z-font 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.