LeetCode ZigZag Conversion C++ 解题思路

一个难度为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"

n=2

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,n是行数。一竖列有n个元素,一斜列第一行和最后一行没有元素所以为n-2个元素,所以加起来周期是2n-2个元素。每行相邻竖列上的元素的距离差别是一个周期,例如当n=4时,周期为2*4-2 = 6, 元素0和6相差6,元素1和7相差6。

在中间的每一行,每两个竖列中间都有一个斜着排列的元素,例如1和7中间是5,7和13中间是11。中间的斜元素和前一个竖列上的元素相差为一个周期-2*行index。可以这样理解,例如,当n=4时,2的下一个周期的元素是8,则2和4的距离就是2到8的距离减去4到8的距离,2到8的距离是一个周期,4到8的距离是1+2+2+……+1,第一个1是第0行的元素,最后一个1是当前行(第i行)的那个元素(即8),中间每行有两个元素,因为i从0开始,所以总共有i个2。所以2到4的距离是2n-2 - 2i,如果2的位置是j,则4的位置是j+2n-2-2i,8的位置是j+2n-2.

代码如下:

string convert(string s, int nRows)
{
    if(s.length() == 0 || nRows<=0)
        return "";
    if(nRows == 1)
        return s;
    string res;
    int size = 2*nRows-2;
    for(int i = 0; i < nRows; i++)
        for(int j = i; j <s.length();j+=size)
        {
            res+=s[j];
            if(i!= 0 && i != nRows-1){
                int temp = j+size-2*i;
                if(temp < s.size())
                    res+=s[temp];
            }
        }
    return res;

}

ref:[1]. http://www.cnblogs.com/springfor/p/3889414.html

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

时间: 2024-10-11 05:46:02

LeetCode ZigZag Conversion C++ 解题思路的相关文章

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

LeetCode ZigZag Conversion 解题报告

对输入字符串,做蛇形变化,然后按行输出. https://oj.leetcode.com/problems/zigzag-conversion/ 例如:The string "PAYPALISHIRING"  的蛇形变化如下: P        A           H        N A   P   L    S     I     I   G Y         I            R 最后要求输出的就是:"PAHNAPLSIIGYIR" Write

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] ZigZag Conversion [9]

称号 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: "PAHNAPL

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

[LeetCode] Longest Valid Parentheses 解题思路

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode] 53. Maximum Subarray 解题思路

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 问题: 给定一个元素有正有负的数组,求最大连续子数组的和. 思路

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

[Leetcode] Backtracking回溯法解题思路

碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大.感觉如果要顺利的把题刷下去,必须先要把做的几道题题总结一下. 先放上参考的web: https://segmentfault.com/a/1190000006121957 http://summerisgreen.com/blog/2017-07-07-2017-07-07-算法技巧-backtr