博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
6. ZigZag Conversion
阅读量:4364 次
发布时间:2019-06-07

本文共 1163 字,大约阅读时间需要 3 分钟。

Description:

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   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 s, int numRows);

  

Examples:

Input: s = "PAYPALISHIRING", numRows = 3Output: "PAHNAPLSIIGYIR"
Input: s = "PAYPALISHIRING", numRows = 4Output: "PINALSIGYAHRPI"Explanation:P     I    NA   L S  I GY A   H RP     I 

Solutions:

 

class Solution(object):    def convert(self, s, numRows):        """        :type s: str        :type numRows: int        :rtype: str        """        if numRows == 1 or numRows >= len(s):  # 如果“行”数为一或者字母数小于“行”数            return s        L = [''] * numRows  #        index, step = 0, 1        for x in s:            L[index] += x   # 不太好理解?            if index == 0:                step = 1            elif index == numRows -1:                step = -1            index += step        return ''.join(L)

  

转载于:https://www.cnblogs.com/qianyuesheng/p/9108321.html

你可能感兴趣的文章
c++ 基础 const char* 转 char*
查看>>
JS-- 小细节--你悟到了什么?
查看>>
收款 借贷
查看>>
Gson关于抽象类的序列化与反序列化
查看>>
Java面向对象之类和对象
查看>>
Oracle数据库提权(dba权限执行系统命令)
查看>>
Hydra爆破神器使用
查看>>
java.util.zip.ZipException: duplicate entry(重复依赖多版本的类库)
查看>>
Run MVC in older version of IIS
查看>>
Ajax 监听
查看>>
隐藏"站长统计"图标
查看>>
Oracle select 中case 的使用以及使用decode替换case
查看>>
创建一个dynamics 365 CRM online plugin (十二) - Asynchronous Plugins
查看>>
Eclipse 常用快捷键 (动画讲解)
查看>>
233 Matrix(矩阵快速幂+思维)
查看>>
Leetcode-Unique Binary Search Trees II
查看>>
Centos7系统下安装Docker
查看>>
PostgreSQL 序列(SEQUENCE)
查看>>
Missing Number
查看>>
Ionic3 demo TallyBook 实例3
查看>>