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

LeetCode # ZigZag Conversion #

时间:2015-04-14 18:02:44      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:


技术分享




我的Python解答:


"""
Programmer  :   EOF
Date        :   2015.04.14
File        :   zigzag.py
E-mail      :   jasonleaster@gmail.com

"""

class Solution:
    def convert(self, s, nRows):
        length = len(s)
        ret_string = ""
        i   = 0
        row = 0 
        counter = 0
        if nRows != 1:
            while row < nRows:
                i = row
                col = 0
                while i < length and counter < length:
                    ret_string += s[i]

                    if row == 0 or row == nRows -1:
                        i += (nRows - 1)*2
                    elif col % 2 == 0:
                        i += (nRows - row -1)*2
                    else:
                        i += row*2
                    col     += 1
                    counter += 1
                row += 1
        else:
            return s

        return ret_string
#----------- just for testing ----------

s = Solution()
print s.convert("AB", 2)
print s.convert("PAYPALISHIRING", 3)
print s.convert("PAYPALISHIRING", 4)


凯旋冲锋的Java解答:


package zigzag_conversion;

public class Solution {
	/*
	0    [         totalGap           ]       6                                         c
	1    [  totalGap-2row  ]    5   [ 2row ]  7                           b             d
	2             4                           8             a                           e
	3                                         9                                         f
	*/
	public String convert(String s, int nRows) {
		if (nRows == 1) {
			return s;
		}
		int len = s.length();
		StringBuilder builder = new StringBuilder(len);
		int totalGap = (nRows - 1) << 1;
		for (int i = 0; i < len; i += totalGap) {
			builder.append(s.charAt(i));
		}
		for (int row = 1; row < nRows - 1; row++) {
			for (int i = row, gap = row << 1; i < len; gap = totalGap - gap, i += gap) {
				builder.append(s.charAt(i));
			}
		}
		for (int i = nRows - 1; i < len; i += totalGap) {
			builder.append(s.charAt(i));
		}
		return builder.toString();
	}

	public static void main(String[] args) {
		System.out.println(new Solution().convert("ABCD", 4));
	}
}


皓神的C++解答:


// Source : https://oj.leetcode.com/problems/zigzag-conversion/
// Author : Hao Chen
// Date   : 2014-07-17

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

#include <iostream>
#include <vector>
#include <string>
using namespace std;

string convert(string s, int nRows) {
    //The cases no need to do anything
    if (nRows<=1 || nRows>=s.size()) return s;
     
    vector<string> r(nRows);
    int row = 0;
    int step = 1;
    for(int i=0; i<s.size(); i ++) {
        if (row == nRows-1) step = -1;
        if (row == 0) step = 1;
        //cout << row <<endl;
        r[row] += s[i];
        row += step;
    }
    
    string result;
    for (int i=0; i<nRows; i++){
        result += r[i];
    }
    return result;
}

int main(int argc, char**argv){

    string s;
    int r;

    s = "PAYPALISHIRING";
    r = 3;
    cout << s << " : " << convert(s, 3) << endl;

}





技术分享




LeetCode # ZigZag Conversion #

标签:

原文地址:http://blog.csdn.net/cinmyheart/article/details/45043529

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