leet code 006: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



Python代码:


def raw_zigzag(n,k):
    li=0
    l=len(n)
    dict01={}
    dict01=dict01.fromkeys(range(k))
    for i in range(k):
        dict01[i]=n[i]
    for j in range(k,l,k+k-2):
        for ii in range(k-1):
            if j+k-2-ii<l:
                dict01[ii]+=' '*(k-2-ii)
                dict01[ii]+=n[j+k-2-ii]
        for ii in range(1,k):
            if j+k+ii-2<l:
                dict01[ii]+=' '*(ii-1)
                dict01[ii]+=n[j+k+ii-2]
    for ll in range(k):
        print(dict01[ll])

str01='PAYPALISHIRINGdaferOHIUHEWIDDFHEAFIUHESHRDFCSDHFIUHFKHDJKCFADGUIPQFGALFHJ'
raw_zigzag(str01,3)

结果:
P A H N f H E D A H R S I K K D P A J
APLSIIGaeOIHWDFEFUEHDCDFUFHJCAGIQGLH
Y I R d r U I H I S F H H D F U F F



猜你喜欢

转载自blog.csdn.net/qq1358223058/article/details/78027417