No.006 ZigZag Conversion

  

6. ZigZag Conversion

  • Total Accepted: 98584
  • Total Submissions: 398018
  • Difficulty: Easy

  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".

  思路:

  首先,我们需要弄清楚这个ZigZag是怎么排列的,这个单词直译过来就是折线、之字形的,在这里不好具体表达,直接放一张图在下面,大家感受一下就知道了。下面分别是行数为5行、8行、10行的情况下的打印图

        

  通过上面的图我们知道,这种排列是有周期的,一个“V”字形为一个周期,我们知道除了第一行和最后一行没有重叠,其他行都有重叠,所以周期为period = 2*nRows-2。所以,我们打印的时候可以之间根据这个周期来进行按行打印。

  我们假设设定的共有n行,

  • 显然,n = 1 时,转换之后的字符串最后就是原先的字符串了
  • 当n > 1时
    • 那么第 0 行,没有重复,所有字符的位置index就是0,0+period,0+2*period。。。即每个周期内添加一个字符
    • 对于第 1 行,显然有重复,所有字符的位置index就是(1,0 +period - 1 ),(1+period,0+2*period-1),(1+2*peroid,0+3*period-1).。。。即每个周期内添加两个字符
    • 。。。
    • 对于第 i 行,显然有重复,所有字符的位置index就是(i,0 +period - i ),(i+period,0+2*period-i),(i+2*peroid,0+3*period-i).。。。即每个周期内添加两个字符
    • 。。。
    • 对于第 (n-1) 行,也就是最后一行,没有重复,所有字符的位置index就是(n-1), (n-1)+ period  , (n-1)+2* period 。。。即每个周期内添加一个字符
    • 对于第0行和最后一行有一个特点就是 (period-i)%peroid == i

  所以,代码如下:

 1 public String convert(String s, int numRows) {
 2     if(s == null || s.length() <= numRows || numRows == 1){
 3         return s ;
 4     }
 5     StringBuilder res = new StringBuilder() ;
 6     char [] arr = s.toCharArray() ;
 7     int period = 2*numRows - 2 ;
 8     for(int i = 0 ; i < numRows ; i++){
 9         for(int j = i ; j < s.length(); j += period){
10             if((period-i)%period != i){
11                 res.append(arr[j]) ;
12                 if((j+period-2*i) < s.length()){
13                     res.append(arr[j+period-2*i]) ;
14                 }
15             }else{
16                 res.append(arr[j]) ;
17             }
18         }
19     }
20
21     return res.toString() ;
22 }
时间: 2024-08-08 05:37:58

No.006 ZigZag Conversion的相关文章

【LeetCode】006 ZigZag Conversion

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

LeetCode 006 ZigZag Conversion - 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: "PAHNAPLSII

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 006.ZigZag_Conversion (Easy) 链接: 题目:https://oj.leetcode.com/problems/zigzag-conversion/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个字符串按横写的折线排列. 分析: 直

Java for LeetCode 006 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: "PAHNAPLSII

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】ZigZag Conversion

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

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

64. ZigZag Conversion

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