码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] ZigZag Conversion

时间:2015-04-17 15:49:24      阅读:144      评论:0      收藏:0      [点我收藏+]

标签: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: "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".

解题思路:

1、一般这种矩形的变换,直接想到的办法是按照它的规则模拟一遍即可。下面是相关代码,其时间复杂度和空间复杂度均为O(n*nRows)

class Solution {
public:
    string convert(string s, int nRows) {
        if(nRows<2){
            return s;
        }
        int len=s.length();
        char matrix[nRows][len];
        for(int i=0; i<nRows; i++){
            for(int j=0; j<len; j++){
                matrix[i][j]=0;
            }
        }
        int colum=0;    //当前放到了第colum列
        int row = 0;    //当前放到了第row行
        bool down = true;   //向下还是向上走
        for(int i=0; i<len; i++){
            matrix[row][colum]=s[i];
            if(down){
                row++;
                if(row==nRows){
                    row=row-2;
                    colum++;
                    down=false;
                }
            }else{
                row--;
                colum++;
                if(row<0){
                    row=1;
                    colum--;
                    down=true;
                }
            }
        }
        string result="";
        for(int i=0; i<nRows; i++){
            for(int j=0; j<len; j++){
                if(matrix[i][j]!=0){
                    result+=matrix[i][j];
                }
            }
        }
        return result;
    }
};
2、看来我发现规律的能力有些欠缺。其实每一竖一提的字符个数为2*nRows-2,其中-2是第一层和最后一层只有一个元素。对于非第一行和非最后一行,元素在s中的下标为j+2*nRows-2-2*i,其中i是当前行,j是当前列。下面是代码。时间复杂度为O(n*nRows),空间复杂度为O(1)。

class Solution {
public:
    string convert(string s, int nRows) {
        if(nRows<2){
            return s;
        }
        int len=s.length();
        int step = 2*nRows-2;
        string result="";
        for(int i=0; i<nRows; i++){
            for(int j=i; j<len; j=j+step){
                result += s[j];
                if(i!=0 && i!=nRows-1 && j+step-2*i<len){   //除了第一行和最后一行,两列之间都有一个字符,其下标为j+step-2*i
                    result += s[j+step-2*i];
                }
            }
        }
        return result;
    }
};


[LeetCode] ZigZag Conversion

标签:c++   leetcode   

原文地址:http://blog.csdn.net/kangrydotnet/article/details/45096037

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!