码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode 6 ZigZag Conversion (C,C++,Java,Python)

时间:2015-05-07 00:59:08      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:c   java   c++   leetcode   python   

Problem:

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

Solution:

纯粹的模拟之字形路线,时间复杂度为O(n)

题目大意:

给一个字符串,按照之字形排列成一个矩阵,然后将矩阵每一行连起来构成一个字符串,求这个字符串,题意不清,还是要给一个图才能明白
0       8       16      
1     7 9     15 17      
2   6   10   14   18      
3 5     11 13     19      
4       12       20      

解题思路:

可以将之字形路线压缩,例如上例中压缩之后的到如下表所示
0   8     16            
1 7 9   15 17            
2 6 10   14 18            
3 5 11   13 19            
4   12     20            
通过这样,然后再来模拟字符串的之字形排列,要注意一些编码细节

Java源代码(用时409ms):

public class Solution {
    public String convert(String s, int numRows) {
        if(numRows==1)return s;
        char[] chs=s.toCharArray();
        if(numRows==2){
            int index=0;
            for(int i=0;i<s.length();i=i+2)chs[index++]=s.charAt(i);
            for(int i=1;i<s.length();i=i+2)chs[index++]=s.charAt(i);
            return new String(chs);
        }
        char[][] map = new char[numRows][s.length()];
        int index=0,col=0;
        while(index<s.length()){
            if(col%2==1){
                int i=numRows-2;
                while(i>=1 && index<s.length())map[i--][col]=s.charAt(index++);
            }else{
                int i=0;
                while(i<numRows && index<s.length())map[i++][col]=s.charAt(index++);
            }
            col++;
        }
        index=0;
        for(int i=0;i<numRows;i++){
            for(int j=0;j<col;j++){
                if(map[i][j]!=0)chs[index++]=map[i][j];
            }
        }
        return new String(chs);
    }
}
C语言源代码(用时79ms):

char* convert(char* s, int numRows) {
    char* ch;
    char map[1000][1000];
    int length=0,index=0,i,j,k;
    if(numRows<=1)return s;
    for(i=0;s[i];i++)length++;
	ch = (char*)malloc(sizeof(char)*length*2);
    if(numRows==2){
        for(i=0;i<length;i=i+2)ch[index++]=s[i];
        for(i=1;i<length;i=i+2)ch[index++]=s[i];
        ch[index]=0;
        return ch;
    }
	for(i=0;i<numRows;i++){
		for(j=0;j<length;j++){
			map[i][j]=0;
		}
	}
    i=0,j=0,index=0;
    while(s[index]){
        if(j%2){
            i=numRows-2;
            while(i>=1 && s[index])map[i--][j]=s[index++];
        }else{
            i=0;
            while(i<numRows && s[index])map[i++][j]=s[index++];
        }
        j++;
    }
    index=0;
    for(i=0;i<numRows;i++){
        for(k=0;k<j;k++){
            if(map[i][k]!=0)ch[index++]=map[i][k];
        }
    }
    ch[index]=0;
    return ch;
}
C++源代码(用时112ms):

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows==1)return s;
        string chs=string(s);
        int index=0;
        if(numRows==2){
            for(int i=0;i<s.size();i=i+2)chs[index++]=s[i];
            for(int i=1;i<s.size();i=i+2)chs[index++]=s[i];
            return chs;
        }
        char map[1000][1000];
        for(int i=0;i<numRows;i++){
            for(int j=0;j<s.size();j++)
            map[i][j]=0;
        }
        index=0;
        int col=0;
        while(s[index]){
            if(col%2){
                int i=numRows-2;
                while(i>=1 && s[index])map[i--][col]=s[index++];
            }else{
                int i=0;
                while(i<numRows && s[index])map[i++][col]=s[index++];
            }
            col++;
        }
        index=0;
        for(int i=0;i<numRows;i++){
            for(int j=0;j<col;j++){
                if(map[i][j])chs[index++]=map[i][j];
            }
        }
        return chs;
    }
};
Python源代码(用时138ms):

class Solution:
    # @param {string} s
    # @param {integer} numRows
    # @return {string}
    def convert(self, s, numRows):
        if numRows==1:return s
        res=["" for i in range(numRows)]
        i=0;gap=numRows-2
        while i<len(s):
            j=0
            while i<len(s) and j<numRows:res[j]+=s[i];i+=1;j+=1
            j=gap
            while i<len(s) and j>0:res[j]+=s[i];i+=1;j-=1
        chs=''
        for i in range(numRows):
            chs+=res[i]
        return chs


LeetCode 6 ZigZag Conversion (C,C++,Java,Python)

标签:c   java   c++   leetcode   python   

原文地址:http://blog.csdn.net/runningtortoises/article/details/45542565

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