这题很有意思,算是一个字符串的重新排列,将字符串按照z的形状进行排列后再逐行进行输出

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

比方说一个字符串下标为1,2,3,4,5,6,7,8,9,10,....根据输入的nRows判断z的行数,

当nRows = 3 时

字符串格式为

1   5   9
2 4 6 8 10
3 7 11

当nRows = 4时

字符串格式为

1     7       13
2 6 8 12 14
3 5 9 11 15
4 10 16

理解题意就很好做

解法如下

我们可以用数组暴力求解时间复杂度为o(n)

建立一个n*nRows的数组去存放,然后逐行读取就ok

 

我用了找规律的方式做这道题,首先我们建立一个不一样的z

假设nRows = 4

1     7       13
2 5 8 11 14
3 6 9 12 15
4 10 16

但是我们先这样看这个z发现每一行的规律就很清晰了

首行与尾行每个字符间的的距离是 nRows*2-2

中间行的每个字符间的距离是是nRows-1

这时我们只需要在这样的格式上根据中心点进行一个对称,每次都去更新其步长就可以得到我们想要的格式

1     7       13
2 6 8 12 14
3 5 9 11 15
4 10 16

是我们要求的z的格式

代码如下所示:

 1 class Solution(object):
 2     def convert(self, s, numRows):
 3         """
 4         :type s: str
 5         :type numRows: int
 6         :rtype: str
 7         """
 8         Slength = len(s)
 9         newStr = ''
10         jump = numRows*2-2
11         if(numRows>=Slength or numRows == 1):
12             return s
13         for i in range(numRows):
14             temp = 1
15             count = i
16             if(i==0 or i==numRows-1):#首行与尾行的间距是不变的
17                 while(count<Slength):
18                     newStr += s[count]
19                     count+=jump
20             else:
21                 if(temp == 1): #需要计算中间行时的第一次步长度
22                     row = 2*i-numRows+1
23                     stage = jump/2-row
24                 while (count < Slength):
25                     temp = 0
26                     newStr += s[count]
27                     count += stage
28                     stage = jump-stage #需要每次更新其步长度,为总长度-之前步长
29         return newStr

这个题不难,只要找到其规律就好做

posted on 2018-01-24 14:40  Qarnet  阅读(110)  评论(0)    收藏  举报