Leetcode # ZigZag Conversion #

Source: Internet
Author: User






My Python solution:


"" "Programmer:eofdate:2015.04.14file:zigzag.pye-mail: [email protected]" "" Class so         Lution: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) * elif Col% 2 = = 0:                    i + = (nrows-row-1) * else:i + = row*2 Col + + 1 Counter + = 1 row + 1 Else:return s return ret_string#-----------Ju St for testing----------S = solution () print S.convert ("AB", 2) Print S.convert ("paypalishiring", 3) print S.convert (" Paypalishiring ", 4)


The Java answer to the Triumph charge:


Package Zigzag_conversion;public class Solution {/*0 [Totalgap] 6                           C1 [Totalgap-2row] 5 [2row] 7 B D2 4                                         8 a E3 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 + = Tota LGAP) {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));}} 


The C + + solution of our Hao God:


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 type this: * (the want to display the this pattern in a fixed font for better leg  ibility) * * 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 Would 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;    The 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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.