国产探花免费观看_亚洲丰满少妇自慰呻吟_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 加一,其他減一


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沁阳市| 平凉市| 措美县| 钦州市| 彩票| 炎陵县| 比如县| 罗田县| 疏勒县| 和顺县| 洮南市| 海伦市| 洪泽县| 垣曲县| 丽江市| 青州市| 尚志市| 腾冲县| 武陟县| 洛南县| 莲花县| 玉门市| 军事| 锦州市| 且末县| 安图县| 辉县市| 延安市| 丰台区| 融水| 京山县| 贵定县| 金秀| 花莲县| 翼城县| 四子王旗| 阜平县| 镇安县| 泗阳县| 五峰| 息烽县|