ZigZag Conversion leetcode java

题目

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
A P L S I I G
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) should return "PAHNAPLSIIGYIR".

 

题解

这道题就是看坐标的变化。并且需要分块处理。

n=2时,字符串坐标变成zigzag的走法就是:

0 2 4 6

1 3 5 7

n=3时的走法是:

0     4     8

1  3 5  7 9

2     6    10

n=4时的走法是:

0      6        12

1   5 7    11 13

2 4   8 10    14

3      9         15

可以发现规律,画红色的长度永远是 2n-2 (想法是你试想把所有这些行压缩成两列,两边手挤一下,第二列永远的第一行和最后一行少字)。

利用这个规律,可以按行填字,第一行和最后一行,就是按照2n-2的顺序一点点加的。

其他行除了上面那个填字规则,就是还要处理斜着那条线的字,可以发现那条线的字的位置永远是当前列j+(2n-2)-2i(i是行的index)。

按照上面的规律就可以写出代码了。

代码如下:

1 public String convert(String s, int nRows) {  
 2         if(s == null || s.length()==0 || nRows <=0)  
 3             return "";  
 4         if(nRows == 1)  
 5             return s;
 6             
 7         StringBuilder res = new StringBuilder();  
 8         int size = 2*nRows-2;  
 9         for(int i=0;i<nRows;i++){  
10             for(int j=i;j<s.length();j+=size){  
11                 res.append(s.charAt(j));  
12                 if(i != 0 && i != nRows - 1){//except the first row and the last row
13                     int temp = j+size-2*i;
14                     if(temp<s.length())
15                         res.append(s.charAt(temp));
16                 }
17             }                  
18         }  
19         return res.toString();  
20     }

Reference:http://blog.unieagle.net/2012/11/08/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Azigzag-conversion/

ZigZag Conversion leetcode java

时间: 2024-12-29 04:44:31

ZigZag Conversion leetcode java的相关文章

ZigZag Conversion leetcode

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 A P L S I I G Y I R And then read line by line: "PAHNAPLSII

LeetCode【6】. ZigZag Conversion --思路图解与java实现

ZigZag Conversion 一.题目如下: 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 A P L S I I G Y I R And then read line

【leetcode刷题笔记】ZigZag Conversion

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 A P L S I I G Y I R And then read line by line:  "PAHNAPLSI

LeetCode ZigZag Conversion(将字符串排成z字型)

1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 string a=""; 5 int len=s.length(); 6 if(len<=nRows||nRows==1) return s; //只有n个,不足一个周期||排成一行 7 int teams=len/(nRows*2-2); //teams个完整的周期 8 int rest=len%(nRows*2-2); //最后一个不足

leetcode -day16 ZigZag Conversion

1.  ZigZag Conversion 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 A P L S I I G Y I R And then read line b

leetCode 6. ZigZag Conversion 字符串 (上传费劲)

6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ string convert2(string s, int numRows) { if (s.length() < 2 || numRows < 2) return s; int cycle = 2 * numRows - 2; string tmp; string result; for (int i = 0; i < numRows; i+

【LeetCode】006 ZigZag Conversion

题目:LeetCode 006 ZigZag Conversion 题意:给一个字符串"PAYPALISHIRING" 按照ZigZag的形式,即按照下面的顺序排列,然后 在转换成一行一行的读取方式,得到"PAHNAPLSIIGYIR".其中行数不定. 思路:肯定是不能去开一个二维数组的,必须要找出规律来直接输出.发现间隔是和总行数numRows有关,另外,第一行和最后一行需要单独考虑,每两个字符间隔一致,为2*(numRows-1):其余行有两种间隔,设当前为第L

LeetCode.ZigZag Conversion ,Container With Most Water

ZigZag Conversion: 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 A P L S I I G Y I R And then read line by lin

Binary Tree ZigZag Level Order Traversal leetcode java

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its