国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

leetcode zigzag conversion(python)

2019-11-08 01:41:41
字體:
來源:轉載
供稿:網友

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   NA P L S I I GY   I   RAnd 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 是什么意思捏:

row=4     06

  15 7

                  24 8

  39

就是這樣子的格式微笑

python 代碼:

class Solution(object):    def convert(self, s, numRows):        """        :type s: str        :type numRows: int        :rtype: str        """        l=[]        for i in range(numRows):            l.append("")        num=numRows*2-2                for i,j in enumerate(s):            if num==0:                return s            elif i%num<numRows:                index=i%num                l[index] +=j            else:                index -=1                l[index] += j        return "".join(l)還看到一位大神的代碼,比我的高明多了。。。。

比numrow小,index 加一,其他減一


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淮北市| 台北市| 鄂州市| 昭觉县| 页游| 甘德县| 龙州县| 鄂尔多斯市| 旌德县| 博罗县| 开江县| 伊春市| 洞头县| 梧州市| 南阳市| 南华县| 莱西市| 溧阳市| 九台市| 蓬安县| 资溪县| 时尚| 平和县| 府谷县| 清新县| 达州市| 双柏县| 南木林县| 当涂县| 昌乐县| 娱乐| 湄潭县| 天台县| 平遥县| 都昌县| 营口市| 育儿| 新邵县| 疏附县| 呼玛县| 利辛县|