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

首先明白题意,给出一个字符串,按照之字形(Zigzag)排列成矩形,将矩阵每一行连接起来构成一个字符串。

将矩阵压缩得到:

 #-*-coding:utf-8-*-

 class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
zigzag = ['' for i in range(numRows)] # 初始化zigzag为['','','']
row = 0 # 当前的行数
step = 1 # 步数:控制数据的输入
for c in s:
if row == 0:
step = 1
if row == numRows - 1:
step = -1
zigzag[row] += c
row += step
return ''.join(zigzag)

leetcode6:Zigzag Conversion@Python的更多相关文章

  1. LeetCode6 ZigZag Conversion

    题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  2. [leetcode]ZigZag Conversion @ Python

    原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...

  3. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

  4. 64. ZigZag Conversion

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  5. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  6. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  7. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  8. 6.[leetcode] ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

随机推荐

  1. CentOS7关闭防火墙方法

    在之前的版本中关闭防火墙等服务的命令是 service iptables stop /etc/init.d/iptables stop 在RHEL7中,其实没有这个服务 [root@rhel7 ~]# ...

  2. 基于XML的AOP配置-转

    http://www.cnblogs.com/yangy608/archive/2010/11/14/1876839.html AOP(Aspect-Oriented Programming,面向切面 ...

  3. Excel 去掉每次打开弹出自定义项安装的弹窗

    弹窗: 解决方案: 一.打开“文件”——“选项”如图. 二.选择“加载项”,下面的“管理”,选择“COM加载项”,然后点击“转到”,弹出框: 三:在“可用加载项”下面你会发现有一项是“LoadTest ...

  4. 【svn】svn 项目地址修改

    1.IDEA修改 确定OK. 2.TortoiseSVN修改 选择项目目录右键->TortoiseSVN->重新定位(Relocate),然后修改URL, 3.Mac OS或Linux客户 ...

  5. mac ssh localhost

    转自:http://blog.csdn.net/cwj649956781/article/details/37913637 mac 无法ssh localhost,错误提示:bash: /usr/lo ...

  6. [Doxygen]Doxygen

    1. Doxygen做什么? 首先这是一个文档生成工具,而不是代码中的注释生成工具.其次,如何生成对应文档,那就是按照一个配置文件中给出的配置格式来书写注释的时候,通过工具就可以解析代码注释最终生成文 ...

  7. php入门part3

    php函数 php函数和JScript的函数大同小异,这里主要强调一下不同之处. 函数的定义:function函数名(形参类表){ 函数体 } 函数的调用:函数名(实参类表) 在php里函数可以先调用 ...

  8. [Mongodb] Tarball二进制包安装过程

    一.缘由: 用在线安装的方式安装mongodb,诚然很方便.但文件过于分散,如果在单机多实例的情况下,就不方便管理. 对于数据库的管理,我习惯将所有数据(配置)文件放在一个地方,方便查找区分. 二.解 ...

  9. ACM入门

    1.给n个数字,将它们重新排序得到一个最大的数字 例子 4123 124 56 90--------------90561241235123 124 56 90 9------------990561 ...

  10. 获取JAVA[WEB]项目相关路径的几种方法

    在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getPro ...