[LeetCode-interview algorithm classic-Java implementation] [006-ZigZag Conversion (Z-type Conversion)], leetcode -- java
[006-ZigZag Conversion (Z-type Conversion )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
The string "PAYPALISHIRING" is written in 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 N
APLSIIG
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
String convert (string text, int nRows );
Convert ("PAYPALISHIRING", 3) shocould return "PAHNAPLSIIGYIR ".
Theme
Enter a string and the specified number of rows, and output the characters in Z.
Solutions
Calculate the maximum number of columns of a character, create a one-dimensional array based on the number of columns and the number of rows, then calculate the position in the one-dimensional array of each character, compact the characters in the one-dimensional array, and return results.
Code Implementation
Public class Solution {public String convert (String s, int nRows) {if (s = null | s. length () <= nRows | nRows = 1) {return s;} int index = s. length (); int rowLength = 0; // calculate the length of the row, including the last line feed character int slash = nRows-2; // a diagonal line removes the number of rows occupied by the beginning and end while (index> 0) {// a vertical column index-= nRows; rowLength ++; // diagonal columns for (int I = 0; I <slash & index> 0; I ++) {rowLength ++; index --;}} char [] result = new cha R [nRows * rowLength]; // an array that stores the result. The last column is used to save the linefeed (int I = 0; I <result. length; I ++) {// Initialization is space result [I] = '';} int curColumn = 0; // index of the number of currently processed rows = 0; while (index <s. length () {// process the vertical line for (int I = 0; I <nRows & index <s. length (); I ++) {result [rowLength * I + curColumn] = s. charAt (index); index ++;} curColumn ++; // process the diagonal line for (int I = nRows-2; I> 0 & index <s. length (); I --) {resul T [rowLength * I + curColumn] = s. charAt (index); curColumn ++; index ++ ;}// System. out. println (new String (result); // compact the character array index = 0; while (index <s. length () & result [index]! = '') {// Locate the character position index ++ of the first space;} int next = index + 1; while (index <s. length () {while (next <result. length & result [next] = '') {// find the element next ++ that is not a space.} result [index] = result [next]; index ++; next ++;} System. out. println (s); System. out. println (new String (result, 0, index); return new String (result, 0, index );}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46938327]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.