题目链接在这里:6. N 字形变换 - 力扣(LeetCode)

比较简单的模拟题,此题需要学习python的字符串处理和数组的处理,字符串的拼接跟string一样直接+就行了,join是将一个数组中的多个字符串拼接起来用的。

1 class Solution(object):
 2     def convert(self, s: str, numRows: int) -> str:
 3         n = len(s)
 4         # print("n:",n)
 5         an = ['' for _ in range(numBows)]
 6         flag, now = 1, 0
 7         for i in range(n):
 8         #     print(s[i],now)
 9             an[now]+=s[i]
10             # print(s[i],now,an[now])
11             if flag == 0:
12                 now+=1
13                 if now == numRows:
14                     now -= 2
15                     flag = 1
16             else:
17                 now-=1
18                 if now < 0:
19                     now += 2
20                     flag = 0
21         ans = ''
22         for i in range(numRows):
23             # print(an[i])
24             ans+=an[i]
25         return ans
26 
27 if __name__=="__main__":
28     s="A"
29     numBows =2
30     # a=''
31     # a+="fuckyourmother"
32     # print(a)
33     ans=Solution.convert(self=0,s=s,numRows=numBows)
34     print(ans)