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

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

官方难度:

Easy

翻译:

字符串"PAYPALISHIRING",被拆成几列写成如下ZigZag模式,写出转译的代码。

例子:

以上的例子如果表达的不够清楚,再给一个清晰的例子:

a     g     m    s     y
b   f h   l n   r t   x z
c e   i k   o q  u w
d     j     p     v
返回"agmsybfhlnrtxzceikoquwdjpv"

思路:

1.用两次遍历,把新字符串加进数组。

2.头尾两行需要特殊处理,间隔是个定值=2*(nRows-1)。

3.根据走势是从下往上还是从下往上,间隔是不同的,使用一个int型的标志位,每次改变之后乘以-1来控制。

解题中可能遇到的困难:

1.内循环的index是i+j,退出条件是i+j<array.length。

解题代码:

 1 private static String method(String text, int nRows) {
 2         char[] array = text.toCharArray();
 3         StringBuffer sb = new StringBuffer();
 4         // 从上至下/从下至上的标志位
 5         int flag = 1;
 6         // 按行遍历
 7         for (int i = 0; i < nRows; i++) {
 8             // 每一行的数据
 9             for (int j = 0; i + j < array.length;) {
10                 sb.append(array[i + j]);
11                 // 极点有特殊的处理方式
12                 if (i == 0 || i == nRows - 1) {
13                     j += 2 * (nRows - 1);
14                 } else {
15                     if (flag == 1) {
16                         // 从上至下处理
17                         j += 2 * (nRows - i - 1);
18                     } else {
19                         // 从下至上处理
20                         j += 2 * i;
21                     }
22                     // 下次改变方向
23                     flag *= -1;
24                 }
25             }
26             // 结束一行遍历,方向标志位还原
27             flag = 1;
28         }
29         return sb.toString();
30     }

测试代码地址:

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q006.java

LeetCode题目地址:

https://leetcode.com/problems/zigzag-conversion/

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

时间: 2024-12-09 23:09:46

No.006:ZigZag Conversion的相关文章

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: "PA

【LeetCode】006 ZigZag Conversion

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

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 bette

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(规律)

题目来源:https://leetcode.com/problems/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

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

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+