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. 白电迁移-Auto fdisk

    ============================== fdisk /dev/vdbnp 1 w fdisk -l cd / mkdir /data mkfs.ext4 /dev/vdb1 mo ...

  2. linux 持续构建(自动部署) 重启动tomcat或进程的脚本

    #!/bin/sh TOMCAT_PATH=`dirname "$0"` echo "TOMCAT_PATH is /usr/local/tomcat" PID ...

  3. 崽崽帮www.zaizaibang.com精选3

    [景山远洋美国交换生随笔]异国他乡的感触 [成都亲子活动]可能是成都最全最好的亲子活动了! 黄平-儿科 @体育活动 下雪天乐翻天之穿越封锁线 北京育翔小学的前世今生 武汉儿科类中医口碑榜 南宁周边农家 ...

  4. WPF中的image控件的Source赋值

    WPF中的Image控件Source的设置 1.XAML中 简单的方式(Source="haha.png"); image控件的Source设置为相对路径后(Source=&quo ...

  5. Validate Disk Failover Failed

    Failed to write file data on cluster disk 0 partition 1, failure reason: The disk structure is corru ...

  6. Ill-conditioned covariance create

    http://www.mathworks.com/matlabcentral/answers/100210-why-do-i-receive-an-error-while-trying-to-gene ...

  7. [SI]source insight使用

    1. 快捷键 Ctrl+O: 工程中查找需要的文件如imx.c Ctrl+F: 当前文件查找字符串,然后Alt+W(hole)可以列出所有找到的位置 Ctrl+/:可以在当前project中查找字符串 ...

  8. Activity Intent Flags及Task相关属性

    转自http://www.cnblogs.com/lwbqqyumidi/p/3775479.html 今天我们来讲一下Activity的task相关内容. 上次我们讲到Activity的四种启动模式 ...

  9. My Interface

    一.创建Myinterface接口 public interface Myinterface { static final String MyifName="我的接口"; stat ...

  10. CSS3常用30种选择器总结

    CSS3常用30种选择器总结 HTML5/CSS3时尚的圆盘时钟动画 带当前日期 www.html5tricks.com/demo/html5-css3-clock-with-date/index.h ...