题目:

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

代码:

看了半天,都没怎看懂题目。查了一下,原来:

Zigzag:即循环对角线结构(

0       8       16      
1     7 9     15 17      
2   6   10   14   18      
3 5     11 13     19      
4       12       20      

这样子的话,就不算麻烦了,只要正确的字符串操作,把每个元素加在正确的位置就好。

于是,共有numRows行,所以定义一个保护numRows元素的数组,用来保存每一行的字符串。

观察规律,第一列从0写到numRows-1,之后从后往前,及numRows-2到0开始逐渐添加。输出并不需要考虑宽度,所以对字符串来说,之后往后加就好

//凑了半天,总算凑出来了

def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        i = 0
        j = 0
        gap = numRows-2
        temp= ['' for x in range(numRows)]
        while i<len(s):
            while(j<numRows):
                if(i>=len(s)):break
                temp[j] += str(s[i])
                j += 1
                i += 1
            j=gap
            while(j>0):
                if(i>=len(s)):break
                temp[j] += str(s[i])
                j -= 1
                i += 1
        result = ''
        for y in temp:result += y

return result

//网上查的,好像更直观   
    def convert2(self, s, nRows):
        if nRows==1: return s
        tmp=['' for i in range(nRows)]
        index=-1; step=1
        for i in range(len(s)):
            index+=step
            if index==nRows:
                index-=2; step=-1
            elif index==-1:
                index=1; step=1
            tmp[index]+=str(s[i])
        return ''.join(tmp)

测试了多次,两个速度差不多,必定时间复杂度和空间复杂度都一样:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOkAAABLCAIAAAAagTdRAAAGDElEQVR4nO1c3dGjMAykrhREPVRDMynmu4cEkKWVLZnMHJrZfbpThPXjxXFgPy9/BFETy/9OgCAmQe4SVUHuElVB7hJVMeLue3stH7y292Hc1+XEuivXy6CcrwGMpxxQjnrZ5ag20JXl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1yeL5jZu0yXpnQhSYm+xbmLASmA3wnixr6CbLF085RAdTBDHPoWJ8gTTxr0KdTVTzRG0ZMfdKF5M2J671uck+0ks3/As8oVQp+3CsPXFVyd9dKA7MavRpNdCE12ITTZhdBkF0KTXQhNdiHcvcWJduTx57hDSQ5XhwIrVPSyhXpbqo4HMy3PdwgJ9HO4d4tzoKk5peyaJKerSzaO2za+OlQzoSU8vcpe6yJg0T+B4hP3bnEKMIXATeS8JIePQ9nNQKdyODXTBKLnlyAQOKojrSDq8OeTfWFoA+Vh8md+efkWVCcCq6oeqpkmENUoXaDsdCfRB6Lt+3HrFucBpnskVYdx9UBgBWsnUTNpIKEfoED2zFlqKqpoED0tVPxv3LrFeRjSBsQfpaSrc4HVVudqZhToaCwMlF/gIi1VB/pR3LvFORqqow+YstWJwIqqB5/H0EB6EvtAo48BRtFBoB/FrVucWFODW6e8JId3XkBgHeue+mXJWkKNEiq5Vx678dbrA5je+1ClFJrsQmiyC6HJLoQmuxCa7EJosgvhLyYpRHTceeTqAAAAAElFTkSuQmCC" alt="" />

6. ZigZag Conversion的更多相关文章

  1. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

  2. 64. ZigZag Conversion

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  3. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  4. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  5. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  6. 6.[leetcode] ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  7. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  8. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  9. leetcode-algorithms-6 ZigZag Conversion

    leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...

  10. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

随机推荐

  1. javascript string 函数集

    JavaScript_String对象说明 string中文为"字符串"的意思,String继承自Object对象,此对象提供字符串的查找操作等函数 JavaScript字符串类型 ...

  2. Mac 下用IDEA时maven,ant打包 (mr 入库hbase)

    现在非常喜欢IDEA,之前在mac 上用的eclipse 经常出现无缘无故的错误.所以转为IDEA.  不过新工具需要学习成本,手头上的项目就遇到了很多问题,现列举如下: 背景描述 在hadoop 开 ...

  3. iOS并发编程指南之同步

    1.gcd fmdb使用了gcd,它是通过 建立系列化的G-C-D队列 从多线程同时调用调用方法,GCD也会按它接收的块的顺序来执行. fmdb使用的是dispatch_sync,多线程调用a ser ...

  4. IE浏览器版本判断

    <script type="text/javascript"> var browser=navigator.appName var b_version=navigato ...

  5. 绝不要进行两层间接非const指针赋值给const指针

    #include <stdio.h> #include <stdlib.h> int main(void) { int *p1; int * *pp1; const int * ...

  6. OpenGL图元的颜色属性

    OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. 1. RGBA颜色RGBA模式中,每一个像素会保存以下数据:R值(红色分量).G值(绿色分量).B值(蓝色分量)和A值(alpha分 ...

  7. php处理数据库数据,每处理一个数据返回客户端显示当前状态的方法。

    php处理大量数据,每处理一个数据返回客户端显示当前状态的方法. 类似于dedecms生成静态页 想法: 客户端发送请求 服务器端接受请求,开始统计所需处理的数据量 将所需处理数据按一定规则排列,发送 ...

  8. BestCoder Round #61 1002 Game

    Problem Description XY is playing a game:there are N pillar in a row,which numbered from 1 to n.Each ...

  9. TCP的几个状态

    转自: TCP的几个状态 (SYN, FIN, ACK, PSH, RST, URG)     http://www.cnblogs.com/lidabo/p/5713569.html

  10. jq隐藏页面的一行

    <script type="text/javascript" src="http://files.cnblogs.com/914556495wxkj/jquery- ...